51

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";

}
5
  • what is max allowed file size set to in apache config Commented May 20, 2015 at 19:40
  • how to check the size? Im not familiar with this.. Commented May 20, 2015 at 19:42
  • I already increase the upload_max_filesize=8M but when i check on phpinfo(); its still 2M . How to solve this? Commented May 20, 2015 at 19:56
  • Is the file to big? Have you tried to upload the file without validating? Commented May 20, 2015 at 19:58
  • 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. Commented Jun 18, 2020 at 15:52

3 Answers 3

107

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 in php.ini

; 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
Sign up to request clarification or add additional context in comments.

5 Comments

When already having a look at the error code, you should also have a look here: 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.
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
also when I use $_FILES['video']['error']; it comes up empty ( 0 )
@JohnnyD, that's normal. An error of 0 means it is okay.
@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
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

3 Comments

How do you know the OP was using WAMP? I don't see any mention of it in the question.
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
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.
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

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.