45

I have this problem on my file upload. I try to upload my PDF file while checking on validation the TMP_NAME is empty and when I check on $_FILES['document_attach']['error'] the value is 1 so meaning there's an error.

But when I try to upload other PDF file it's successfully uploaded. Why is other PDF file not?

HTML

<form action="actions/upload_internal_audit.php" method="post" enctype="multipart/form-data">
   <label>Title</label>
   <span><input type="text" name="title" class="form-control" placeholder="Document Title"></span>  
   <label>File</label>  
   <span><input type="file" name="document_attach"></span><br>
   <span><input type="submit" name="submit" value="Upload" class="btn btn-primary"></span>
</form>

PHP

if(isset($_POST['submit'])){

$title = $_POST['title'];
$filename = $_FILES['document_attach']['name'];
$target_dir = "../eqms_files/";
$maxSize = 5000000;

if(!empty($title)){

    if(is_uploaded_file($_FILES['document_attach']['tmp_name'])){
        if ($_FILES['document_attach']['size'] > $maxSize) {
                echo "File must be: ' . $maxSize . '";
        } else {

                $result = move_uploaded_file($_FILES['document_attach']['tmp_name'], $target_dir . $filename);
                mysqli_query($con, "INSERT into internal_audit (id, title, file) VALUES ('', '".$title."', '".$filename."')");
                echo "Successfully Uploaded";
        }   
    }else
        echo "Error Uploading try again later";

}else
    echo "Document Title is empty";

}
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
Jay Viluan
  • 1,571
  • 3
  • 12
  • 16
  • what is max allowed file size set to in apache config – Dimi May 20 '15 at 19:40
  • how to check the size? Im not familiar with this.. – Jay Viluan May 20 '15 at 19:42
  • I already increase the upload_max_filesize=8M but when i check on phpinfo(); its still 2M . How to solve this? – Jay Viluan May 20 '15 at 19:56
  • Is the file to big? Have you tried to upload the file without validating? – Jakob Leifhelm May 20 '15 at 19:58
  • https://www.php.net/manual/en/features.file-upload.errors.php lists all error codes. `1` is `UPLOAD_ERR_INI_SIZE`: The uploaded file exceeds the `upload_max_filesize` directive in `php.ini`. You might need to restart apache or php-fpm to use new `php.ini` settings. – JonnyJD Jun 18 '20 at 15:52

3 Answers3

100

I just check the max size in phpinfo();

Then check if php.ini is loaded

$inipath = php_ini_loaded_file();

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

Then Change the upload_max_filesize=2M to 8M

; Maximum allowed size for uploaded files.
upload_max_filesize = 8M 

; Must be greater than or equal to upload_max_filesize
post_max_size = 8M 

Finally reset your Apache Server to apply the changes

service apache2 restart
Colin McGovern
  • 105
  • 1
  • 6
Jay Viluan
  • 1,571
  • 3
  • 12
  • 16
  • When already having a look at the error code, you should also have a look here: https://www.php.net/manual/en/features.file-upload.errors.php. And `post_max_size` should always be a bit bigger than `upload_max_filesize`, otherwise `upload_max_filesize` will never "trigger" because the file is included in the POST, plus a bit of extra metadata. – JonnyJD Jun 18 '20 at 15:55
  • I am facing the same issue though upload is enabled and upload is set in the .user.ini to 500M, still the `$uservid = $_POST['video']['tmp_name'];` while I am not facing the same issue with the images. Also I get not errors what so ever in the error_log @jay-viluan – JohnnyD Jul 27 '22 at 08:11
  • also when I use `$_FILES['video']['error'];` it comes up empty ( 0 ) – JohnnyD Jul 27 '22 at 08:15
  • @JohnnyD, that's normal. An error of 0 means it is okay. – no ai please Jul 27 '22 at 18:11
  • @Someone_who_likes_SE yeah I know that, the issue was the above but it seems I had a `$_FILES ` replaces with a `$_POST` which was causing me the issue – JohnnyD Jul 29 '22 at 07:26
12
var_dump($_FILES['file_flag']['tmp_name']); // file_flag file key 
will return empty string 
array (size=1)
  'course_video' => 
    array (size=5)
      'name' => string 'fasdfasdfasdfsd.mp4' (length=19)
      'type' => string '' (length=0)
      'tmp_name' => string '' (length=0) <===== *** this point 
      'error' => int 1
      'size' => int 0

This happen because WAMP server not accepting this much size to uploaded on server.

to avoid this we need to change php.ini file.

upload_max_filesize=100M (as per your need)
post_max_size = 100M (as per your need)

In Wamp Server

Change Post Max Size

Change Max Upload Size

finally restart server

Restart WAMP Server

  • How do you know the OP was using WAMP? I don't see any mention of it in the question. – OMA Oct 23 '17 at 11:08
  • 2
    hello OMA , you only have to find php.ini file whether it is LAMP,XAMP,WAMP and you have to make changes in max_upload and post_max flag as per your requirements – Rajesh Prasad Yadav Oct 23 '17 at 11:14
  • 3
    Yes, I know, thanks. I was just asking because you are giving very specific indications for WAMP, when the OP didn't talk anything about using WAMP. – OMA Oct 24 '17 at 11:48
1

another option is to add a separate config file with upload limits.

i created uploads.ini:

memory_limit = 64M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 60

and placed it in conf.d directory

in my case using Docker: /usr/local/etc/php/conf.d/uploads.ini

that way i could keep my production php.ini and add this just for uploads control

Sonic Soul
  • 23,855
  • 37
  • 130
  • 196