When I upload an imagen with PHP, I get no errors and the image is 0 bytes on server.
Things considered:
File_uploads is On in phpinfo()
Upload_max_filesize is 8M in phpinfo()
Upload_tmp_dir is set and has 777 permisions
If I run file_exists($_FILES['userfile']['tmp_name']), it returns
true
If i run getimagesize($_FILES['userfile']['tmp_name']), I get an Read
Error notice. The image is valid and can be opened with system image
viewer
I am using a very simple upload script for testing:
HTML:
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="512000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>
PHP:
<?php
//$uploaddir = '/var/www/html/upload';
$uploaddir = '';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
echo "<p>";
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>
When I submit the file, I get no errors and the image is created in the destination folder, with the correct name and extension, but as the title says, it is 0 bytes (of course, it can't be oppened).
Thanks for your time.
EDIT
Sajad Karuthedath just made me see that $_FILE['file']['size'] value is 0 !! What can be the problem causing files are not being uploaded correctly?
EDIT 2
Uploading txt, pdf, tar files is working properly.. The problem is only with image type files!
try this simple upload script ,
you should check for errors before saving to server
if ( 0 < $_FILES['file']['error'] ) {
echo 'Error: ' . $_FILES['file']['error'] . '<br>';
}
else {
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/'.$_FILES['file']['name']);
}
Note: don't forget to restart your server or xampp after changing phpinfo()
Note: also change post_max_size = 25M in php
In order to track the issue just add an "exit;" straight after the "move_uploaded_file..." command and if the file is properly uploaded it's the code after the "move_upload" which is wrong.
In my case I was opening the uploaded file after upload with an overwrite (w+) option in order to check the lock state of the file. which was making a empty file.
Related
I am trying to create a webpage to upload an image from iPhone's photo library or take an image using iPhone's camera.
This is the error message on my iphone
This is the html code to upload the image
<html>
<form action="test_upload_image_results.php" method="post" enctype="multipart/form-data">
Select image to upload:
<br><input type="file" name="imageUpload" id="imageUpload">
<br><br><input type="submit" value="Upload Image" name="submit">
</form>
</html>
This is the php code (test_upload_image_results.php) used to upload the image
<html>
<body>
<?php
print_r($_FILES);
$tmpName = $_FILES["imageUpload"]["tmp_name"];
$destDir = "uploads/";
if (!is_dir($destDir))
{
throw new Exception('Destination is not a directory.');
}
if (!is_writable($destDir))
{
throw new Exception('Destination directory is not writable.');
}
$destination = $destDir.basename($_FILES["imageUpload"]["name"]);
if (is_file($destination))
{
throw new Exception('Destination filename already exists.');
}
if (move_uploaded_file($tmpName, $destination))
{
echo "done";
}
else
{
throw new Exception('Unable to move file.');
}
?>
</body>
</html>
Sorry if this is a very elementary question.
Because your php ini config upload_max_filesize is lower than size of uploading image.
Try to change upload_max_filesize to bigger one in php.ini
For more, see file eupload error codes at php.net doc http://php.net/manual/en/features.file-upload.errors.php
Looking at the error, there is no value set for $imageUpload['tmp_name'] and size is 0 - the image has not been uploaded
The error 1 indicates
"The uploaded file exceeds the upload_max_filesize directive in php.ini"
See http://php.net/manual/en/features.file-upload.errors.php
I want to upload the files in html as follow :
<tr><td><?php _e("Upload Trust Logo","emarksheet"); ?></td><td><label class="btn btn-danger" for="file-sel"><input id="file-sel" type="file" name="imaget" style="display:none;" size="25" />Browse to Upload Logo ....</label></td></tr>
<tr><td><?php _e("Upload Institute Logo","emarksheet"); ?></td><td><label class="btn btn-primary" for="file-sel2"><input id="file-sel2" type="file" name="image" style="display:none;" size="25" />Browse to Upload Logo ....</label></td></tr>
Two upload these files. My php code is as follow :
$path = plugin_dir_path(__FILE__);
$file = $_FILES['image'];
//print_r($_FILES);
$name1 = $file['name'];
$type = $file['type'];
$size = $file['size'];
$tmppath = $file['tmp_name'];
move_uploaded_file ($tmppath, $path.'logos/'.$name1);
//upload data end
//upload trust logo`
$file2 = $_FILES['imaget'];
//print_r($_FILES);
$name2 = $file2['name'];
$type2 = $file2['type'];
$size2 = $file2['size'];
$tmppath2 = $file2['tmp_name'];
move_uploaded_file ($tmppath2, $path.'logos/'.$name2);
When I upload the files. The file name with $name2 is uploaded but the file with name $name1 is not uploaded
Please help why it not be uploaded
As you posted, the 'image' image has an error 4 code. That means no file was uploaded, as seen here:
http://www.php.net/manual/en/features.file-upload.errors.php
Why?
Since you are getting info in $_FILES, then you are not posting a total amount of data larger than the post_max_size php.ini directive, as, in this case, $_FILE would be completelly empty.
Then, maybe the php.ini upload_max_filesize, the maximum size for a singe file, is exceeded or the max_file_uploads is set to one, as max_file_uploads is the maximum number of files allowed to be uploaded simultaneously.
Check those params at http://php.net/manual/en/ini.core.php and in your php.ini file.
I tried this snippet in my localhost server, and placed in [www_root]/tests/uploads/test1.php (note I just used the relevant part for our case):
<?php
if (empty($_FILES)) {
echo "<form enctype='multipart/form-data' method='POST' action='http://localhost/tests/uploads/test1.php'>";
echo '<input id="file-sel" type="file" name="imaget" size="25" />';
echo '<input id="file-sel2" type="file" name="image" size="25" />';
echo '<button type="submit">Submit</button>';
} else {
echo print_r($_FILES);
}
And both files where correctly uploaded. Then the problem must be in the PHP configuration, not in the actual PHP code.
My values in php.ini:
post_max_size=8M
upload_max_filesize=2M
max_file_uploads=20
This means I can upload 20 files at the same time, but they can not exceed 8M in total nor an individual file can exceed 2M.
EDIT: after modifying the php.ini file, the server must be restarted in order to get those new values loaded.
Hope it helps.
I have an HTML form as the following:
<form id="addTrack" action="/worship/script/upload.php" method="post" enctype="multipart/form-data">
<label>File:</label>
<input type="file" name="uploaded" id="addTrackFile"/>
<label>Key Title: </label>
<input type="text" name="title" id="addTrackTitle"/>
<input type="hidden" name="id" id="addTrackId"/><br>
</form>
<button onclick="uploadAddTrack()">Upload</button>
<button onclick="closeAddTrack()">Close</button>
When I submit the form the file uploads to the server properly, but when it gets redirected to the PHP action script, it gets stopped at the first error catch. The script then dumps the $_FILES variable which it returns as an empty array. As you can see in the code below, I also have it echo the error, but it also echoes an empty string.
Why am I not getting a file in the $_FILES array?
My PHP Code:
$id=$_POST["id"];
$name=$_POST["title"];
$name = str_replace(" ","",$name);
$allowed_filetypes = array('.mp3','.m4a','.wav','.wma');
$filename = $_FILES['uploaded']['name'];
$ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
$target = "../audio/";
$target = $target . $id. "_".$name.$ext;
$ok=1;
if ($_FILES['uploaded']['error'] !== UPLOAD_ERR_OK) {
//------------This is where it gets stopped-----------------//
var_dump($_FILES);
echo $_FILES["uploaded"]["error"];
return;
}
if(!in_array($ext,$allowed_filetypes))
die("This file type is not allowed");
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
include("updateDB.php");
header("Location:/worship/cpanel/?autoload=$id");
}
The size of the file I am uploading is 9mb.
My php.ini relevant info
file_uploads: On
upload_max_filesize: 25M
upload_tmp_dir: no value
max_post_size: 8M
check you PHP.ini file. make sure the POST size is larger the 8M. because that is the default and you're sending info that is 9MB.
`; Maximum size of POST data that PHP will accept.
post_max_size = 8M`
I'm trying to build a form to upload a thumbnail (image) to the server - looks like the image is successfully uploading to the servers temp folder (I've confirmed this with get_defined_vars - its giving a temp file name and no errors), but its not being moved to the folder in the DOCUMENT_ROOT
The end goal here is to get the image uploaded then return a url to be stored in MySQL
HTML Form (located in DOCUMENT_ROOT/admin)
<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="thumbnail">Thumbnail </label>
<input type="file" name="thumbnail" id="thumbnail">
</form>
Upload.php
<?php
$thumbnail = uploadfile($_FILE['thumbnail']['name'],$_SERVER['DOCUMENT_ROOT'].'/thumbs/upload/',$_FILE['thumbnail']['tmp_name']);
if(($thumbnail)!== FALSE)
{echo $thumbnail;} else {echo 'upload failed<br>';}
function uploadfile($origin, $dest, $tmp_name)
{
$origin = strtolower(basename($origin));
$fulldest = $dest.$origin;
$filename = $origin;
echo '$fulldest '.$fulldest.'<br />';
echo '$filename '.$filename.'<br />';
if (move_uploaded_file($tmp_name, $fulldest))
{return $filename;}
return false;
}
?>
result (note: I've removed the actual document root)
$fulldest [DOCUMENT_ROOT] /thumbs/upload/
$filename
upload failed
I've also set an .htaccess directive for upload 500M to make sure image size isn't the problem
<IfModule mod_php4.c>
php_value upload_max_filesize 500M
php_value post_max_size 500M
</IfModule>
EDIT:: My target directory is chmod 0777 for testing - so permissions shouldn't be the problem.
I can't help noticing that your $filename is empty and $fulldest lack the filename. I think the problem is that you use $_FILE and not $_FILES.
$thumbnail = uploadfile($_FILES['thumbnail']['name'],
$_SERVER['DOCUMENT_ROOT'].'/thumbs/upload/',
$_FILES['thumbnail']['tmp_name']);
I think that would work.
Here is the HTML:
<h1>Upload Custom Logo</h1>
<form action="settings.php" enctype="multipart/form-data" method="post">
<input type="file" name="Logo" />
<input type="submit" value="Upload" name="logoUpload" />
</form>
Here is the PHP:
<?php /* Handle Logo Upload */
if($_POST['logoUpload']) {
$target_path = "uploads/";
$Logo = $_FILES['Logo']['name'];
$target_path = $target_path . basename( $_FILES['Logo']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo '<div class="statusUpdate">The file '. basename( $_FILES['Logo']['name']).
' has been uploaded</div>';
}
} else{
echo '<div class="statusUpdate">There was an error uploading the file to: '.$target_path.', please try again! Error Code was: '.$_FILES['Logo']['error'].'</div>';
}
}
?>
It always goes to the "else" statement on that code, I have an uploads folder in the same directory as this php file set to 777 file permissions. The image I am test uploading is under 10KB in size. But the move_uploaded_file() always fails and it doesn't return any error message except for the custom error message made with the else statement.
You're referring to the file in the $_FILES array by two different names -
$Logo = $_FILES['Logo']['name'];
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)
Which one is it? Logo or uploadedfile ?
You're probably referencing an array index which doesn't exist.
Possible reasons:
The uploading of the file failed in some way.
You don't have permission to move the file to the target path.
As PHP.NET says:
If filename is not a valid upload file, then no action will occur, and
move_uploaded_file() will return FALSE.
If filename is a valid upload file, but cannot be moved for some
reason, no action will occur, and move_uploaded_file() will return
FALSE. Additionally, a warning will be issued.
Reference: http://php.net/manual/en/function.move-uploaded-file.php