我正在使用以下PHP脚本上传多个文件:
<?php
if(isset($_FILES['uploadfile'])){
$total_files = count($_FILES['uploadfile']['name']);
if( $total_files > 0){
for($i=0; $i<$total_files; $i++) {
$file_name = $_FILES['uploadfile']['name'][$i];
$file_size = $_FILES['uploadfile']['size'][$i];
$file_tmp = $_FILES['uploadfile']['tmp_name'][$i];
$file_type = $_FILES['uploadfile']['type'][$i];
$upload_Path = "storage/".$file_name;
//var_dump($file_size);
//die;
if($file_size > 8000000){
echo ('Total upload size must be less than 8 MB.');
die;
}
if($file_tmp == ""){
echo ('There is no file path.');
die;
}
else{
if(!file_exists($upload_Path)){
move_uploaded_file($file_tmp, $upload_Path);
}
else{
$name = pathinfo($file_name, PATHINFO_FILENAME);
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$new_name = $name.rand().'.'.$ext;
$new_Path = "storage/".$new_name;
move_uploaded_file($file_tmp, $new_Path);
}
}
}
}
die('File uploaded successfully!');
}
?>
但问题是,每当发生错误时,例如echo ('Total upload size must be less than 8 MB.');
,它都不会使用ajax输出。但是,成功上传后,会显示File uploaded successfully!
。
我的AJAX如下:
$.ajax({
type:'POST',
url: 'mupld.php',
data: formdata,
processData:false,
contentType:false,
success: function(response){
alert('Success: '+response);
},
error: function(xhr, status, error){
alert('Error: '+status+' '+error);
}
});
在进行var转储时,我没有得到任何高于8mb的上传输出但是低于我得到的
Success: <pre class='xdebug-var-dump' dir='ltr'><small>int</small> <font color='#4e9a06'>3283515</font>
</pre>
答案 0 :(得分:3)
@Jeff Bucket是对的,所以我编辑了我的答案:
实际上,您应该在成功回调中处理这些错误。 error()回调保留用于浏览器和服务器之间的连接刚刚中断的情况,并且error()参数期望处理这种情况,例如典型的textStatus错误应该是'Not Found'或'Internal Server Error ',但没有'总上传大小必须小于8 MB。'。
你应该返回一个包含你可以在客户端使用的信息的数组,并在success()中处理它,如:
尝试{
if(isset($_FILES['uploadfile'])){
$total_files = count($_FILES['uploadfile']['name']);
if( $total_files > 0){
for($i=0; $i<$total_files; $i++) {
$file_name = $_FILES['uploadfile']['name'][$i];
$file_size = $_FILES['uploadfile']['size'][$i];
$file_tmp = $_FILES['uploadfile']['tmp_name'][$i];
$file_type = $_FILES['uploadfile']['type'][$i];
$upload_Path = "storage/".$file_name;
//var_dump($file_size);
//die;
if($file_size > 8000000){
echo json_encode( array('status' => 'failure' , 'msg' => 'Total upload size must be less than 8 MB.') );
die();
}
if($file_tmp == ""){
echo json_encode( array('status' => 'failure' , 'msg' => 'There is no filepath.') );
die;
}
else{
if(!file_exists($upload_Path)){
move_uploaded_file($file_tmp, $upload_Path);
}
else{
$name = pathinfo($file_name, PATHINFO_FILENAME);
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$new_name = $name.rand().'.'.$ext;
$new_Path = "storage/".$new_name;
move_uploaded_file($file_tmp, $new_Path);
}
}
}
}
echo json_encode( array('status' => 'success' , 'msg' => 'File uploaded succesfully.') );
die();
}
else{
echo json_encode(array("status" => "error" , "msg" => "No file was found when processing uploaded files" ) );
die();
}
}
catch(Exception $ex){
echo json_encode(array('status' => 'error' , 'msg' => 'An unhandled exception raised: ' . $ex->getMessage() ) );
die();
}
finally{
die();
}
然后在$ .ajax()函数中:
$("#uploadfile").change(function(){
//submit the form here
var files = $("#fileupload")[0];
var formdata = new FormData(files);
$.ajax({
type:'POST',
url: 'mupld.php',
data: formdata,
processData:false,
contentType:false,
success: function(response){
response = JSON.parse(response);
alert(response.msg);
},
error: function(xhr, textStatus, error){
console.log('Error: '+textStatus+' '+error);
}
});
如果您特别想在error()回调中处理此问题,则应将php脚本的响应代码设置为500或任何自定义代码 - 使用header()。