5
votes

I'm trying to upload a file using PHP! I've tried uploading PNG,JPG,PDF,TXT files, these uploads only works when the file size is around 20kb. When I try to upload files where its size is around 150KB, it prints $_FILE error = 3 and the file name lets say '1234.png' where the tmp_name is empty and the image itself as well! here's my code

ini_set('display_errors',1);
error_reporting(-1);

$imageTmp = addslashes($_FILES['image']['tmp_name']);
//$imageTmp = $_FILES['image']['tmp_name'];
$imageOldName = addslashes($_FILES['image']['name']);
$imageData = file_get_contents($imageTmp);
//$imageData = base64_encode($imageTmp);

echo 'image temp name: '. $imageTmp .' ';
echo 'error: '. $_FILES['image']['error']. ' ';
echo 'image name: '. $imageOldName. ' ';
echo 'image data: '. $imageData. ' ';
echo 'image type:'. $_FILES['image']['type'];
echo "<pre>";
echo "POST:";
print_r($_POST);
echo "FILES:";
print_r($_FILES);
echo "</pre>";


$inipath = php_ini_loaded_file();

if ($inipath) {
echo 'Loaded php.ini: ' . $inipath;
} else {
echo 'A php.ini file is not loaded';
}

I've tried multiple solutions such as changing the values of post_max_size = 200M and upload_max_filesize = 200M instead of 32M

Here's is the result of trying to upload txt file 4KB:

image temp name: /Applications/MAMP/tmp/php/phpOc7d6a error: 0 
image Name: test.txt image data: hello image type:text/plain
POST:Array
(
[submit] => Record Test
)
FILES:Array
(
[image] => Array
    (
        [name] => test.txt
        [type] => text/plain
        [tmp_name] => /Applications/MAMP/tmp/php/phpOc7d6a
        [error] => 0
        [size] => 405
    )

  )  
  Loaded php.ini: /Applications/MAMP/bin/php/php5.6.10/conf/php.ini

the result of uploading PNG file 127KB:

Warning: file_get_contents(): Filename cannot be empty in path/test1.php on line 10 image temp name: error: 3 image name: IMG_8807.JPG image data: image type:

POST:Array
(
)
FILES:Array
(
[image] => Array
    (
        [name] => IMG_8807.JPG
        [type] => 
        [tmp_name] => 
        [error] => 3
        [size] => 0
    )

)
Loaded php.ini: /Applications/MAMP/bin/php/php5.6.10/conf/php.ini

BTW, after a failed upload, the server crashes and display 502 Gateway and I have to restart the Apache!

1
What are the settings for max upload file size in your php.ini? Are you sure you changed the right php.ini? Did you restart Apache after you made the change? - Jay Blanchard
do you get the value of upload_max_filesize as 200M when you call phpinfo(). - Anish Silwal
@ASK yup, both upload max size and post max size are 200 - Nawaf Altharwy
@JayBlanchard upload_max_filesize = 200M, the reason i echoed the inipath was to make sure i'm editing the right one ,yes restarted apache afterward - Nawaf Altharwy

1 Answers

0
votes

As php documentation says:

UPLOAD_ERR_PARTIAL is given when the mime boundary is not found after the file data. 
A possibly cause for this is that the upload was cancelled by the user (pressed ESC, etc).

Maybe:

  • Permissions are wrong

  • Not enough free space on server.

  • Uploading from iOS.

  • This error can occur when uploading a folder due to browser limitations. Happens on Mac OS X.

These are some things you could try. Hope this helps.