Javascript 使用PHP通过Ajax上传文件
我试图使用XMLHttpRequest函数上传一个文件来运行php脚本。 下面是HTML代码Javascript 使用PHP通过Ajax上传文件,javascript,php,ajax,file-upload,Javascript,Php,Ajax,File Upload,我试图使用XMLHttpRequest函数上传一个文件来运行php脚本。 下面是HTML代码 <form enctype="multipart/form-data" action="testupload.php" method="POST"> <input type="text" class="form-control input-lg" name="image_tex
<form enctype="multipart/form-data" action="testupload.php" method="POST">
<input type="text" class="form-control input-lg" name="image_text" id="filename">
<input type="hidden" name="MAX_FILE_SIZE" value="51200000000" />
Send this file: <input name="usrfile" type="file" />
<input type="button" value="Send File" onclick="uploadtest()"/>
</form>
这是我的PHP脚本
<?php
/if(!empty($_FILES['usrfile'])){
// print_r($_FILES);
// echo "goda";
// // File upload configuration
$targetDir = "C:/wamp64/www/CMS_TEST2/uploads/";
$allowTypes = array('mp4', 'jpg', 'png', 'jpeg', 'gif');
// $fileName = basename($_FILES['file']['name']);
$fileName = $_GET["fileID"];
// $fileName = "MCCT0001";
$temp = explode(".", $_FILES["usrfile"]["name"]);
$newfilename = $fileName.".".end($temp);
$targetFilePath = $targetDir.$newfilename;
// Check whether file type is valid
$fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);
if(in_array($fileType, $allowTypes)){
// Upload file to the server
if(move_uploaded_file($_FILES['usrfile']['tmp_name'], $targetFilePath)){
echo "File is valid, and was successfully uploaded.\n";
print_r($_FILES);
}
}
//}
?>
如果我使用php isset表单submit调用php脚本。文件上传工作。但是,当我使用ajax$\文件调用php脚本时,问题是返回空。你能帮我解决一下我这里做错了什么吗第一个问题是你在AJAX请求中使用了GET方法。GET请求不能有请求正文。更准确地说,客户机可以发送 GET请求有一个主体,但服务器被实现为忽略它。 请注意,在表单中使用POST方法 第二个问题是,您实际上没有在JavaScript代码中包含文件数据。下面是一个示例:查看如何使用FormData在POST请求中发送文件 函数sendFilefile{ consturi=/index.php; const xhr=新的XMLHttpRequest; const fd=新表单数据; openPOST,uri,true; xhr.onreadystatechange=函数{ 如果xhr.readyState==4&&xhr.status==200{ alertxhr.responseText;//处理响应。 } }; fd.append'myFile',file; //启动多部分/表单数据上载 xhr.sendfd; }
作为补充说明,对于这种操作,我建议查看HTML5文件API,读取文件的字节,并将它们发送到服务器。您还可以将字节分块以发出更多web请求,以支持上载任何大小的文件。尝试使用JSON encode echo JSON\u encode arrayfile=>$\u文件发送响应,并检查是否收到响应。您得到的是文件名,而不是文件本身。这可能对你有帮助。您可以通过检查页面来检查您的ajax是否在chrome控制台上工作。ajax正在工作,因为我可以看到所有回音响应都在页面控制台中打印。我仍然无法获得此工作函数的上载测试{const fileInput=document.querySelectorfileInput;console.logttest函数;const fd=new FormData;var xmlhttp=new XMLHttpRequest;xmlhttp.onreadystatechange=function{if this.readyState==4&&this.status==200{console.logthis.responseText;}};fd.append'file',fileInput;xmlhttp.openPOST,testupload2.php,true;xmlhttp.sendfd;}我得到的usrfile未定义php错误不管怎样,非常感谢您的解释,我也得到了一个完整的示例代码,我很高兴我能提供一些帮助。我认为您得到的usrfile未定义,因为您应该在FormData中配置此名称,所以:fd.append'usrfile',file;-我从MDN网站复制了示例,但没有更新它在PHP代码中匹配服务器端期望的字段名
<?php
/if(!empty($_FILES['usrfile'])){
// print_r($_FILES);
// echo "goda";
// // File upload configuration
$targetDir = "C:/wamp64/www/CMS_TEST2/uploads/";
$allowTypes = array('mp4', 'jpg', 'png', 'jpeg', 'gif');
// $fileName = basename($_FILES['file']['name']);
$fileName = $_GET["fileID"];
// $fileName = "MCCT0001";
$temp = explode(".", $_FILES["usrfile"]["name"]);
$newfilename = $fileName.".".end($temp);
$targetFilePath = $targetDir.$newfilename;
// Check whether file type is valid
$fileType = pathinfo($targetFilePath, PATHINFO_EXTENSION);
if(in_array($fileType, $allowTypes)){
// Upload file to the server
if(move_uploaded_file($_FILES['usrfile']['tmp_name'], $targetFilePath)){
echo "File is valid, and was successfully uploaded.\n";
print_r($_FILES);
}
}
//}
?>