PHP文件上传报错tmp_name为空

新手上路,请多包涵

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

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

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

}

原文由 Jay Viluan 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 761
2 个回答

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

然后检查是否加载了php.ini

 $inipath = php_ini_loaded_file();

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

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

最后重置您的 Apache 服务器以应用更改

service apache2 restart

原文由 Jay Viluan 发布,翻译遵循 CC BY-SA 4.0 许可协议

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

发生这种情况是因为 WAMP 服务器不接受上传到服务器的这么多大小。

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

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

在 Wamp 服务器中

更改帖子最大大小

更改最大上传大小

最后重启服务器

重启 WAMP 服务器

原文由 Rajesh Prasad Yadav 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏