Javascript js发出的php文件上传信号

Javascript js发出的php文件上传信号,javascript,php,file,fwrite,Javascript,Php,File,Fwrite,我从js收到了一篇XMLHttprequest.send的帖子。 当我在Chrome中按F12时,我可以看到POST发送正常。 但是,当调用我的php处理程序并尝试将发送的image.png写入tmp.png文件时,结果文件大小为零。我做错了什么? [已编辑] 我更改了php处理程序。新版本如下。抛出异常,表示参数无效 这是我的密码: js: 剪贴板_write.php: <?php try { // Undefined | Multiple Files | $_FILES Cor

我从js收到了一篇XMLHttprequest.send的帖子。 当我在Chrome中按F12时,我可以看到POST发送正常。 但是,当调用我的php处理程序并尝试将发送的image.png写入tmp.png文件时,结果文件大小为零。我做错了什么? [已编辑] 我更改了php处理程序。新版本如下。抛出异常,表示参数无效

这是我的密码:

js:

剪贴板_write.php:

<?php
try {
    // Undefined | Multiple Files | $_FILES Corruption Attack
    // If this request falls under any of them, treat it invalid.
    if (
        !isset($_FILES['upfile']['error']) ||
        is_array($_FILES['upfile']['error'])
    ) {
        throw new RuntimeException('Invalid parameters.');
    }
?>

通常,您可以使用JavaScript执行以下操作:

//{
get=(url、成功、上下文)=>{
const x=新的XMLHttpRequest;
常数c=上下文| | x;
x、 打开('GET',url);
x、 onload=()=>{
if(success)success.call(c,JSON.parse(x.responseText));
}
x、 send();
}
post=函数(url、发送、成功、上下文){
const x=新的XMLHttpRequest;
常数c=上下文| | x;
x、 打开('POST',url);
x、 onload=()=>{
if(success)success.call(c,JSON.parse(x.responseText));
}
if(typeof send=='object'&&send&!(数组的发送实例)){
if(发送FormData的实例){
x、 发送(send);
}
否则{
const fd=新表单数据;
for(让k输入发送){
append(k,JSON.stringify(send[k]);
}
x、 发送(fd);
}
}
否则{
抛出新错误(“发送参数必须是对象”);
}
返回x;
}
doc=document;html=doc.documentElement;bod=doc.body;nav=navigator;M=tag=>doc.createElement(tag);I=id=>doc.getElementById(id);
mobile=nav.userAgent.match(/Mobi/i)?真:假;
S=(选择器,在范围内)=>{
var w=在| | doc内;
返回w.querySelector(选择器);
}
Q=(选择器,内部)=>{
var w=在| | doc内;
返回w.querySelectorAll(选择器);
}
aC=函数(){
常量a=[].slice.call(参数),n=a.shift();
n、 添加(…a);
返回aC;
}
rC=函数(){
常量a=[].slice.call(参数),n=a.shift();
n、 类列表。删除(…a);
返回rC;
}
tC=函数(){
常量a=[].slice.call(参数),n=a.shift();
n、 切换(…a);
返回tC;
}
//魔法就在这里发生
const upload=I('upload'),upload_error=I('upload_error');
upload.onfocus=()=>{
rC(upload_error,'ya');upload_error.textContent='file required';
}
upload.onchange=函数(){
让files=this.files,file,fd;
if(files.length){
if(this.value.match(/\(png|jpe?g|gif|bmp)$/i)){
file=文件[0];
如果(file.size>10485760){
upload_error.textContent='文件必须小于等于10 MB';
}
否则{
fd=新表单数据;fd.append('uploaded\u img',file);
upload_error.textContent='正在等待服务器';
post('yourPage.php',fd,o=>{
//o将是响应对象
如果(o.成功){
upload_error.textContent='upload success';aC(upload_error,'good');
}
否则{
//哈克斯有哈克斯
}
这个。blur();
});
}
}
否则{
upload_error.textContent='必须是公共图像扩展';
}
}
}
upload.onerror=函数(){
this.blur();upload_error.textContent='upload error';
}
}); // 端荷载
//]]>
*{
框大小:边框框;
}
html,正文{
背景:#ccc;
}
呃{
颜色:#900;左边距:7px;
}
呃,很好{
颜色:#090;
}


所需文件
文件上载在
$\u文件
中,而不是
$\u POST
@Barmar I已更改链接中的代码。现在它给了我异常无效参数。为什么?您是否将
$\u文件['upfile']
更改为
$\u文件['photo']
,以匹配您为
formData.append()
指定的参数?所有像
s
Q
这样的神秘名称是怎么回事。?这里似乎有很多代码与上传过程没有真正的联系,并且模糊了答案。我把它们放进去了,以防OP想使用
.querySelector
.queryselectoral
。这样OP就可以得到他们想要的任何元素。此外,它还显示了操作的技巧。阅读只需几秒钟。
<?php
try {
    // Undefined | Multiple Files | $_FILES Corruption Attack
    // If this request falls under any of them, treat it invalid.
    if (
        !isset($_FILES['upfile']['error']) ||
        is_array($_FILES['upfile']['error'])
    ) {
        throw new RuntimeException('Invalid parameters.');
    }
?>