1

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!

8
  • 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 Commented Mar 18, 2015 at 14:25
  • Try $uploaddir = 'upload/'; if running your script from the root of your server. Commented Mar 18, 2015 at 14:26
  • 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. Commented Mar 18, 2015 at 14:34
  • did you restart your server after changing max upload size ?? @dieguit Commented Mar 18, 2015 at 14:36
  • 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. Commented Mar 18, 2015 at 14:38

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thank you for the answer. File has no error, this a dump of the object: [userfile] => Array ( [name] => logo_pers.jpg [type] => image/jpeg [tmp_name] => /var/www/tmp/php32ogoT [error] => 0 [size] => 0 )
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.