[英]PHP file upload error tmp_name is empty
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.我尝试上传我的 PDF 文件,同时检查验证
TMP_NAME
是否为空,当我检查$_FILES['document_attach']['error']
时,值是 1,这意味着有错误。
But when I try to upload other PDF file it's successfully uploaded.但是当我尝试上传其他 PDF 文件时,它已成功上传。 Why is other PDF file not?
为什么其他 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";
}
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
Finally reset your Apache Server to apply the changes最后重置您的 Apache 服务器以应用更改
service apache2 restart
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)
finally restart server最后重启服务器
Increase the following in your php.ini file after that you can now get the tmp_name of the uploaded file 之后,在php.ini文件中增加以下内容,现在您可以获取上载文件的tmp_name
upload_max_filesize= (as per your need) post_max_size = (as per your need) upload_max_filesize =(根据您的需要)post_max_size =(根据您的需要)
another option is to add a separate config file with upload limits.另一种选择是添加一个带有上传限制的单独配置文件。
i created uploads.ini
:我创建了
uploads.ini
:
memory_limit = 64M
upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 60
and placed it in conf.d
directory并将其放在
conf.d
目录中
in my case using Docker: /usr/local/etc/php/conf.d/uploads.ini
在我的例子中使用 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这样我就可以保留我的作品 php.ini 并添加它只是为了上传控制
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.