File upload results in file size 0 and error code

2019-07-31 10:32发布

I'm trying to upload an image to a php script. I have a, non-persistent, bug that results in some of the images uploaded has a file size of 0. I have tried to print the _FILES array to my log file, and it shows the error code being 0, which should be ok.

These lines:

foreach($_FILES['image_file'] as $key => $val){
      error_log(date('j/n/o H:i:s')." ". $key ." => ".$val. "\n", 3,$log_path);
    }

Give me these in the log file:

  1. 3/10/2012 12:12:54 name => 59175248636.jpg
  2. 3/10/2012 12:12:54 type => image/jpeg
  3. 3/10/2012 12:12:54 tmp_name => C:\WINDOWS\Temp\php411F.tmp
  4. 3/10/2012 12:12:54 error => 0
  5. 3/10/2012 12:12:54 size => 0

As can be read from the log file, this script runs on a Windows machine, of which I have limited knowledge. I have already changed the post_max_size to 10M, as well as upload_max_size to 10M in the php.ini.

I am flabbergasted about this issue. When I test from my own devices, it works fine, but for some reason, when my testers try it out, it fails.

标签: php file upload
1条回答
该账号已被封号
2楼-- · 2019-07-31 11:20

EDIT - Try this:

I think you may need to change $_FILES['file'] to $_FILES['image_file'] to work with your setup. I have left it as below for ease of reading...

if ($_FILES['file']['error'] === UPLOAD_ERR_OK && $_FILES['file']['size'] > 0){

   ... upload was successful ...

}elseif($_FILES['file']['error'] === UPLOAD_ERR_OK && $_FILES['file']['size'] == 0){

   die("Upload failed, random upload issue... please try again);
   // change this error to something more useful... (leave this error in for testing)

}else {

   die("Upload failed with error code " . $_FILES['file']['error']);

}

This doesn't solve the random filesize issue, but it will allow your code / process to continue. Treat it as a failed upload and tell the user to try again...

Used code from this stackoverflow post posted by Marc B

查看更多
登录 后发表回答