gpt4 book ai didi

php - Ajax成功函数不输出Php脚本发送的文本

转载 作者:行者123 更新时间:2023-12-01 07:45:58 26 4
gpt4 key购买 nike

我正在使用下面的 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 ('总上传大小必须小于 8 MB.'); 之类的错误时,它都不会使用 ajax 输出。但上传成功后,会显示文件上传成功!

我的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 的文件,我没有得到任何输出,但对于低于 8mb 的文件,我得到了

Success: <pre class='xdebug-var-dump' dir='ltr'><small>int</small> <font color='#4e9a06'>3283515</font>
</pre>

最佳答案

@Jeff Bucket 是对的,所以我编辑了我的答案:

实际上,您应该在成功回调中处理这些错误。 error() 回调保留用于浏览器和服务器之间的连接刚刚中断的情况,并且 error() 参数期望处理此类情况,例如典型的 textStatus 错误应该是“未找到”或“内部服务器错误” ”,但没有“总上传大小必须小于 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() 回调中处理此问题,则应使用 header() 将 php 脚本的响应代码设置为 500 - 或任何自定义代码。

关于php - Ajax成功函数不输出Php脚本发送的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37909272/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com