【问题标题】:how to get http request upload file name at php?如何在 php 中获取 http 请求上传文件名?
【发布时间】:2020-10-27 21:25:57
【问题描述】:
function fileupload () {
var data = new FormData(),
PMfile = document.getElementById("PMfile").files;
data.append("file", PMfile[0]);
var xhr = new XMLHttpRequest();
xhr.open("POST", "upload.php", true);
xhr.send(data);
return false;
}
<input onchange="fileupload()" type="file" id="PMfile" name="PM_file" class="form-control" required>
我想在upload.php获取文件名
我试过$filename = $_FILES['file']['name'];,但这不起作用。
【问题讨论】:
标签:
javascript
php
file-upload
xmlhttprequest
【解决方案1】:
这是正确的,应该可以工作。
转储整个 $_FILES 并检查你是否得到了你想要的。
对我来说,它显示了这个:
Array ( [file] => Array ( [name] => example.jpg [type] => image/jpeg [tmp_name] => /tmp/phpBQOHQl [error] => 0 [size] => 48316 ) )
所以$_FILES['file']['name'] 将是example.jpg
但是如果我上传的文件对于我当前的 php 设置来说太大了,我会得到 $_FILES 作为Array ( ) 所以它是空的,并且在 apache 日志中我有:
[Tue Jul 07 21:04:25.535907 2020] [:error] [pid 7729] [client 127.0.0.1:37234] PHP Warning: POST Content-Length of 21138710 bytes exceeds the limit of 8388608 bytes in Unknown on line 0, referer: http://localhost/a.php
所以我的猜测是 - 代码是正确的(如果 js/html 中没有错误,您不会向我们展示)但上传有错误 - 请检查您的错误日志。