1
votes

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!

1
No errors, because you're not checking for them, or your system's settings are enabled for it. php.net/manual/en/function.error-reporting.php - Funk Forty Niner
Try $uploaddir = 'upload/'; if running your script from the root of your server. - Funk Forty Niner
Hi Fred, there are no errors on the logs. Also, trying error_reporting(E_ALL) in the script still does not show anything. The upload dir is not the issue, since it only sets where the new file is created, and the script is creating the file correctly (with 0 bytes size) on the root or upload folder. - dieguit
did you restart your server after changing max upload size ?? @dieguit - Sajad Karuthedath
They probably are not getting displayed. Try error_reporting(E_ALL); ini_set('display_errors', 1);. Also try $uploadfile = $uploaddir ."/" . basename($_FILES['userfile']['name']); but your $uploaddir = ''; is empty, so that could be a factor. - Funk Forty Niner

1 Answers

0
votes

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