php PHP文件上传错误tmp_name为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30358737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 21:44:45  来源:igfitidea点击:

PHP file upload error tmp_name is empty

phphtml

提问by Jay Viluan

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

我的文件上传有这个问题。我尝试在检查验证时上传我的 PDF 文件,该文件TMP_NAME为空,当我检查$_FILES['document_attach']['error']该值为 1 时,这意味着存在错误。

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

但是当我尝试上传其他 PDF 文件时,它已成功上传。为什么其他PDF文件不是?

HTML

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

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

}

回答by Jay Viluan

I just check the max size in phpinfo();

我只是检查 phpinfo() 中的最大大小;

Then check if php.ini is loaded

然后检查php.ini是否加载

$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

然后更改 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 

回答by IMRA

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.

发生这种情况是因为 WAMP 服务器不接受在服务器上上传这么大的尺寸。

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

为了避免这种情况,我们需要更改 php.ini 文件。

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

In Wamp Server

在 Wamp 服务器中

Change Post Max Size

更改帖子最大尺寸

Change Max Upload Size

更改最大上传大小

finally restart server

最后重启服务器

Restart WAMP Server

重启 WAMP 服务器