使用PHP做好上传大文件。我在php.ini中设置了upload_max_filesize和post_max_size的值:
upload_max_filesize = 64M
post_max_size = 64M
但是使用Ajax无法加载大文件。使用Ajax正确上传的文件少于5 KB。
对于大文件,我在控制台中收到以下错误:
对象{readyState:4,responseText:“”,状态:200,statusText:“确定”}
如何使用以下代码上传大文件?
我的ajax代码:
$("#submit-edit-order").click(function () {
//*****get data input
var formData = new FormData();
formData.append('action', 'update_order');
formData.append('order_id', $('input[name=order_id]').val());
formData.append('customer_name', $('input[name=customer_name]').val());
formData.append('customer_id', $('input[name=customer_name]').data("cid"));
formData.append('date', $('input[name=date]').val());
formData.append('order_status', $('#order_sataus').find(":selected").val());
formData.append('total_price', $('input[name=totalprice]').val().replace(/,/g, ''));
formData.append('quits', $('input[name=quits]').val().replace(/,/g, ''));
formData.append('desc', $('#desc').val());
formData.append('desc2', $('#desc2').val());
$.each($("input[type=file]"), function (i, obj) {
$.each(obj.files, function (j, file) {
formData.append('orderpic[]', file);
});
});
//ajax start and ajax complatet
ajax_loading();
$.ajax({
url: "../../includes/ajax/ajax.php",
data: formData,
processData: false,
contentType: false,
type: 'POST',
dataType: 'json',
success: function (response) {
if (response.type == 'error') {
output = '<div class="alert alert-danger" style="margin-top:12px;">' + response.text + '</div>';
} else {
location.reload();
output = '<div class="alert alert-success" style="margin-top:12px;">' + response.text + '</div>';
}
$("section").find("#results").html(output).fadeIn('slow');
},error: function(data){
console.log(data);
}
});
});
PHP代码:
if($_POST['action']=='update_order'){
$order_id = $db->escape($_POST['order_id']);
$customer_id = $db->escape($_POST['customer_id']);
$date = explode("/", $_POST['date']);
$date = jDateTime::toGregorian($date[0], $date[1], $date[2]);
$date = $date[0]."/".$date[1]."/".$date[2];
$status = $_POST['order_status'];
$total_price = $_POST['total_price'];
$quits = $_POST['quits'];
$desc = $_POST['desc'];
$desc2 = $_POST['desc2'];
//file settings
$files = array();
$uploaddir = '../../uploads/orders/';
for($i=0; $i<count($_FILES['orderpic']['name']); $i++) {
//Get the temp file path
$tmpFilePath = $_FILES['orderpic']['tmp_name'][$i];
//Make sure we have a filepath
if ($tmpFilePath != "") {
//Setup our new file path
$time = time();
$ext = pathinfo($_FILES['orderpic']['name'][$i], PATHINFO_EXTENSION);
//$FilePath = $uploaddir . $time .'.'. $ext;
$FilePath = $uploaddir . $time. basename($_FILES['orderpic']['name'][$i]);
//Upload the file into the temp dir
if(move_uploaded_file($tmpFilePath, $FilePath)){
$resizeObj = new resize($FilePath);
$resizeObj -> resizeImage(200, 350, 'portrait');
$newFilePath = $uploaddir ."200_350". $time. basename($_FILES['orderpic']['name'][$i]);
$resizeObj -> saveImage($newFilePath, 100);
unlink($FilePath);
$files[] = "200_350". $time. basename($_FILES['orderpic']['name'][$i]);
}
}
}
$files = implode("|", $files);
$data = array(
"user_id"=>$customer_id,
"productImgLink"=>$files,
"totalPrice"=>$db->escape($total_price),
"quits"=>$db->escape($quits),
"orderDesc"=>$db->escape($desc),
"orderDesc2"=>$db->escape($desc2),
"status"=>$db->escape($status),
"date"=>$db->escape($date)
);
$db->where("order_id",$order_id);
$res = $db->update("orders",$data);
if (!$res) {
$output = json_encode(array('type'=>'error', 'text' => 'fail'));
die($output);
} else {
$output = json_encode(array('type'=>'message', 'text' => 'Done.'));
die($output);
}
}