我正在制作一个基本的Web界面来上传文件,我的按钮成功发送一个带有包含图像数据的有效负载的POST请求,但在PHP脚本中,$ _FILES数组为空 . 我使用chrome dev工具检查我的html和javascript,但没有错误 . 此外,我检查了apache错误日志,也没有错误,所以我有点不知道出了什么问题 .
我正在关注这个例子并略微修改它 .
这是html和javascript
<html>
<head>
Image Uploader
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<h1> Upload Your Images Here </h1>
<input type="file" id="fileElem" style="display:none" onchange="handleFiles(this.files)">
<button id="fileSelect"> Select some files </button>
<script>
$("#fileSelect").click(function()
{
$("#fileElem").click();
});
function handleFiles(image)
{
console.log("Handling Files");
var reader = new FileReader();
var xhr = new XMLHttpRequest();
xhr.open("POST", "getImage.php");
xhr.overrideMimeType('text/plain; charset=x-user-defined-binary');
reader.onload = function(evt)
{
xhr.send(evt.target.result);
console.log("sent");
};
reader.readAsBinaryString(image[0]);
}
</script>
</body>
</html>
这是php脚本 .
<?php
print_r($_FILES);
print_r($_POST);
print(count($_FILES));
echo "Reached End";
?>
这是我从脚本中回复的响应 . 它只显示空数组 .
Array
(
)
Array
(
)
0Reached End
我尝试了不同类型的文件,每次都得到相同的空数组 . 我确保在php.ini中启用了文件上传,并且帖子和文件大小限制足够高(80M) . 如果它有助于我运行一个apache2服务器与php 7.0安装所有在树莓派 .
我还检查了apache错误日志,但没有错误 .
1 回答
index.php
upload.php
Upload File without using Form Submit in Ajax PHP