javascript - 如何在 php 中获取 http 请求上传文件名?
问题描述
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'];
,但这不起作用。
解决方案
这是正确的,应该可以工作。转储整个 $_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 中没有错误,你不会向我们展示)但上传有错误 - 检查你的错误日志。
推荐阅读
- reactjs - React this.context 在 React Native 0.61 中返回空对象 {}
- java - 我可以在 zk 的 hbox 中使用包含吗?
- python - 如何使用“columns.str.replace”替换数据集中的每个列名,其中包含 500 列?
- oracle - alter a trigger with different name
- http - 我应该将 Cache-Control: no-cache 添加到我的 REST API 的 GET 端点吗?
- c++ - 该库无法在 Qt 中链接
- c# - 使用 C# 获取 RabbitMQ 服务器上所有已声明交换的列表
- c# - 如果我超过 Google SMTP 发送限制,我的 C# 代码会发生什么
- python - 使用 Python Flask 时 Localhost:5000 继续加载
- shopify - 循环浏览特定集合中的产品