我有一个接受多个文件的输入:
<input id="propertyImages" type="file" name="submission_img[]" multiple accept=".jpg, .jpeg, .png, .gif"/>
然后我通过JS / Ajax将这些数据发布到PHP文件:
//Compiling the data which will be POSTed using AJAX. Note that the input above with id of propertyImages is not part of the form with id accommodationForm and is therefore appended to the formData.
var form = $("#accommodationForm")[0];
var formData = new FormData(form);
var propertyImages = document.getElementById("propertyImages");
for(var i=0; i<propertyImages.files.length; i++){
formData.append('propImages[]', propertyImages.files[i]);
}
//Printing to console the formData entries to confirm the formData data:
for (var d of formData.entries()) {
console.log(d);
}
//The partial code for the ajax call:
$.ajax({
url: "includes/handlers/ajax_submit_accommodation.php",
method: "POST",
data: formData,
...
将数据发布到的PHP代码:
$errorstatus = 0; //Used as an error flag
$maximagesize = 10485760; // Max file size allowed = 10MB
$acceptableimage = array( //The allowed mime types
'image/jpeg',
'image/jpg',
'image/png',
);
$submission_images_array = array();
$output = ""; //Used for debugging purposes
for($x=0; $x<30; $x++){ //30 is the upper limit for the amount of images POSTed
if($_FILES['propImages']['size'][$x] != 0){
if( $_FILES['propImages']['size'][$x] >= $maximagesize || (!in_array($_FILES['propImages']['type'][$x], $acceptableimage))) {
$errorstatus = 1;
} else {
$submission_img = $_FILES['propImages']['name'][$x];
$submission_img = pathinfo($submission_img, PATHINFO_FILENAME);
$submission_img = $submission_img.'-'.$user_id.'-'.rand().'.jpeg';
array_push($submission_images_array, $submission_img);
$submission_img_tmp = $_FILES['propImages']['tmp_name'][$x];
compressImage($submission_img_tmp, '../../submitted_media/accommodation/'.$submission_img, 50);
$output .= "$x:"." ".$submission_img."\n";
}
} else {
$output .= "$x: Empty image\n";
$submission_img = "";
array_push($submission_images_array, $submission_img);
}
}
file_put_contents('../../filenames.txt', $output ); // DEBUG
我希望用户最多附加30张图像。我确保前端和后端对此进行检查。我遇到的问题是我可以附加30张图像-调试通过打印确认到formData
...的控制台,即... formData
包含所有图像数据。但是,当我查看登录PHP的filenames.txt
时,它仅包含前20个文件名,而后10个条目则记录为“空图像”(如代码中我所指示的,如果图像大小是0)。
注意:
我已经确保(使用其他JS代码)propertyImages.files.length
循环中的for
不能大于30,并且在附加30张图像时,其长度确实为30。
我已经确定PHP.ini文件中的max_input_vars
不是问题。
最后10个文件的图像大小不为0。这已通过记录formData
答案 0 :(得分:1)
看看max_file_uploads指令:
max_file_uploads 整数
允许同时上传的最大文件数。
如“文件上传”部分中的表格所示,默认值为20:
Name Default Changeable Changelog
...
max_file_uploads 20 PHP_INI_SYSTEM Available since PHP 5.2.12.
您需要更新您的php.ini
的值。
还请检查您没有超过post_max_size或 upload_max_filesize。