i got a error in image uploading on window based server where it works well on localhost as well as in linux server.Firstly i manually create a folder named as photos in root directory & two files named as imageupload.php & index.php
where code is
index.php
<html>
<body>
<form enctype="multipart/form-data" action="imageupload.php" method="post">
Choose Picture:<input type="file" name="photo"/>
<input name="submit" type="submit" value="save"/>
</form>
</body>
</html>
imageupload.php
<?php
if(isset($_REQUEST['submit'])){
$target = "photos/";
$finallink = $target.basename($_FILES['photo']['name']);
if(move_uploaded_file($_FILES['photo']['tmp_name'], $finallink))
{
echo "The file ". basename( $_FILES['photo']['name']). " has been uploaded, and your information has been added to the directory";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
}
?>
this code works well on localhost & as well as on linux server but i purchased a window based hosting & where as I'm getting error on running this code on window based web server is .
Sorry, there was a problem uploading your file.
How to fix it ? Is this issue of php code running on window based server or anything else? Thanks for your help in advance.
Try this code hope it will hlp you
if(isset($_REQUEST['submit'])){ move_uploaded_file($_FILES["photo"]["tmp_name"],
"upload/" . $_FILES["photo"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["photo"]["name"]; }
move_uploaded_file($_FILES["companylogo"]["tmp_name"],"companylogo/" . $_FILES["companylogo"]["name"]);
echo "Stored in: " . "companylogo/".$_FILES["companylogo"]["name"];
Related
I'm currently working on an extremely simplistic PHP file upload, but it fails to do anything at all. There is nothing in the specified directory.
PHP on the submit of my HTML form:
<?php
if ($_FILES["file"]["error"] > 0)
{
//error
echo "Something went wrong";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"/uploads/" . $_FILES["file"]["name"]);
}
?>
I suspect that something may be up with the fact that I'm using GoDaddy, since they've been quirky with php features in the past.
EDIT: Fixed underscore;
now I'm getting an actual error.
Warning: move_uploaded_file(/uploads/asd.docx) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/content/09/11461509/html/php/upload.php on line 11
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpSIA5Jv' to '/uploads/asd.docx' in /home/content/09/11461509/html/php/upload.php on line 11
You're running your code from a different folder "php", from what I've gathered by your error messages. Try running the code from the root of your server, and then use uploads as your uploading folder.
Also make sure the folder has the proper write permissions set. Usually 755 and sometimes 777 although 755 is a safer setting.
As per OP's original posted code
Missing underscore between $ and FILES
move_uploaded_file($FILES["file"]["tmp_name"],
---^
<?php
if ($_FILES["file"]["error"] > 0)
{
//error
echo "Something went wrong";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"/uploads/" . $_FILES["file"]["name"]);
}
?>
Please add the enctype in form tag as multipart/form-data and PHP code should as follows-
First make sure that your files has been uploaded successfully in temp directory, then try to debug upload path and finally file moving functionality to specific directory.
<?php
if ($_FILES["file"]["error"] > 0) {
//error
echo "Something went wrong";
} else {
// try echoing the following codes first-
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
// then try to debug upload path
// move_uploaded_file($_FILES["file"]["tmp_name"],"/uploads/" . $_FILES["file"]["name"]);
// take move_uploaded_file(); in a variable and then print_r($variable);
}
?>
I am referring you to read this post for upload problem the docx file using PHP.
Try this
<html>
<body>
<form action="" method="post"enctype="multipart/form-data">
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
?>
please write in form tag enctype="multipart/form-data"
//uploadForm.html
<html>
<body>
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<label for="browseFile">Filename : </label>
<input type="file" name="file" id="browseFile"><br>
<input type="submit" name="submit" value="Submit">
</body>
</html>
//upload_file.php
<?php
$allowedExt = array("png","jpg");
$temp = explode(".",$_FILES["file"]["name"]);
$extension = end($temp);
echo "uploading...";
if((($_FILES["file"]["type"]=="image/png") || ($_FILES["file"]["type"]=="image/jpg")) && ($_FILES["file"]["size"] < 1000000))
{
echo "success";
if($_FILES["file"]["error"] > 0)
{
echo "error in uploading" . $_FILES["file"]["error"]."<br>";
}
else
{
echo "<p>uploaded successfully</p>";
}
}
else
echo "invalid file" ;
echo $_FILES["file"]["name"]."stored in ".$_FILES["file"]["tmp_name"]."<br>";
move_uploaded_file($_FILES["file"]["tmp_name"],"uploads/".$_FILES["file"]["name"]);
echo "moved Successfully";
?>
When I try to echo the temp directory name , it is blank . The uploaded files are missing .
I dont get it in the MAMP/htdocs folder neither in /tmp/ directory .
I dont have uploads directory in /MAMP/htdocs/ .Wont the program create a directory if it does not exist ?
In your final instructions, you have $_FILES['name']['tmp_name'] instead of $_FILES['file']['tmp_name'].
By the way, you have a few errors in your script:
Even if someone uploads an invalid file, you show them an error message, but you still move it to the final place.
$_FILES["file"]["type"] is a value sent by the browser (ie: the client). A malicious attacker may sent you any kind of file and disguise it as a image/png, and you are trusting it. You cannot trust this value. Instead, you could use getimagesize, which returns you an array that has the mime type of the image (and is detected by the server (ie: by you). To detect the mime-type of non-images, you can use FileInfo, concretely finfo_file.
Also, the php script will not create your uploads folder if it does not exist, and instead will show an error (and do nothing). You must create this folder first, and make sure that the user running your php script (usually the same that is running your http server) has write permissions on that directory.
edit: You don't see any uploaded file in your temp directory because (quoting http://www.php.net/manual/en/features.file-upload.post-method.php):
The file will be deleted from the temporary directory at the end of
the request if it has not been moved away or renamed.
$allowedExt = array("png","jpg");
echo $temp = explode(".",$_FILES["file"]["name"]);
$extension = end($temp);
echo "uploading...";
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
$_FILES["name"]["tmp_name"] does not exist, it should be $_FILES["file"]["tmp_name"]
I am trying to make a simple uploading application from a web page:localhost/test.html. I am getting these errors:
Warning: move_uploaded_file(test/Blue hills.jpg): failed to open stream:
No such file or directory in C:\wamp\www\test.html on line 11
and
Warning: move_uploaded_file(): Unable to move 'C:\wamp\tmp\php376.tmp'
to 'test/Blue hills.jpg' in C:\wamp\www\test.html on line 11
Here is my code
<html>
<form enctype="multipart/form-data" action="test.html" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>
<html>
<?php
$target = "test/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else {
echo "Uploading Error.";
}
Probably the directory test doesn't exist. Add these lines to your code.
if (file_exists('test/')) echo 'Ok it wasn\'t that';
else echo 'Um, create a directory called test here: '.dirname(__FILE__);
Ensure that the a test/ directory exists in the directory where your script is located, then you can use
$out = dirname(__FILE__) . '/' . $_FILES['uploaded']['name'];
move_uploaded_file($_FILES['uploaded']['tmp_name'], $out);
Change directory permissions (CHMOD) to 777 via your FTP client (read,write,execute for owner,group,public).
I am encountering a strange problem with my script which I am testing to upload PDF files. I can sucessfully upload some pdf files while not the other files, even though they are all pdfs and have .pdf as extension. Can anyone throw some light on this after going thtough my code
HTML PART:
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="file" name="upload" /><br />
<input type="submit" name="submit">
PHP PART:
if(isset($_POST['submit'])){
$output_form = 0;
if (($_FILES["upload"]["type"] == "application/pdf")
&& ($_FILES["upload"]["size"] < 80000)){
if (file_exists("upload/" . $_FILES["upload"]["name"]))
{
echo $_FILES["upload"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["upload"]["tmp_name"],
"upload/" . $_FILES["upload"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["upload"]["name"];
}
}else{
echo 'Invalid File';
}
}
For some files I am getting the output, stored in output. For the others I am getting the message 'Invalid File'.
Thanks
your code above seems to have a condition that if the filesize is greater than 80000 then it should throw the 'Invalid file' error? What size are the ones that fail? I'd be willing to bet if you comment out that condition it'll work
Had the same issues.
Found that the file type could also be application/x-octet-stream
So you need to check for that in the same statement that you are checking the file size.
Something like this:
if (($_FILES['pdfUpload']['type'] == "application/pdf")
|| ($_FILES['pdfUpload']['type'] == "application/x-octet-stream")
&& ($_FILES['pdfUpload']['size'] < 9000000)) //Much larger and we get a timeout during transfer
My 2 cents worth
in the following code ,i am trying to upload an image to server . there is a folder 'images ' on the server. whenever i am clicking the 'ok' button ...it gives an error " there is problem in uploading file"... where is the problem in my code?
html-----------------------------------------
<form method="post" action="newproduct.php" enctype="multipart/form-data">
Item Image:<input type="file" name= "photo" size="40" />
Description:<textarea name="description" cols="40" rows="1"></textarea>
<input name="submit" type="submit" value = "Submit" />
</form>
php-------------------------------------------------------
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);
$pic=($_FILES['photo']['name']);
$description =$_POST["description"];
//checking for empty values
if (empty($pic) || empty($description))
{
echo "Please enter all field values.";
}
else
{
//Connecting to database server
//Connecting to database
//INSERT Query
$SQLstring = "INSERT INTO items VALUES(null,'$pic' ,'$description')";
$QueryResult = #mysqli_query($DBConnect, $SQLstring)
or die ("<p> Unable to execute the query. </p>".
"<p> Error code " . mysqli_errno($DBConnect) . ":" . mysqli_error($DBConnect))."</p>";
if(move_uploaded_file($_FILES['photo']['name'], $target))
{
echo "The file has been added to the directory";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
mysqli_close($DBConnect);
}
?>
The images folder needs to have 777 permissions on it. By default the permissions are 655, and PHP does not have permissions to upload/move/copy a file outside the current folder it is in (subdirectories count as different folder)
you can't do that... you don't update a photo like that in Javascript... (with ajax..)
It is not going to work like that...
But... you can fake this...
You have to submit that post somehow...
You have several solutions: use a flash to fake upload ajax or an iframe..
You can also use jQuery .. he will do all the stuff for you..
Here are some demo & download links:
http://www.phpletter.com/Demo/AjaxFileUpload-Demo/
http://www.webdeveloperjuice.com/2010/02/13/7-trusted-ajax-file-upload-plugins-using-jquery/
http://www.fyneworks.com/jquery/multiple-file-upload/
If you want to move the uploaded file you have to move the "tmp_name" with the new name.. like
if (!move_uploaded_file($_FILES['photo']['tmp_name'], $path.$_FILES['photo']['name']))
echo 'CANNOT MOVE {'.$_FILES['photo']['name'].'}' . PHP_EOL;
When you upload your file, apache takes care of it and by default is in /tmp (if you use linux... I don't know in windows case)..
P.S: for your script's performance you should use ' ' instead of " " for strings.. when you use " " PHP is checking every " "(string) for variables == more operations to do.. and ' ' are skipped