繁体   English   中英

通过 AJAX 将数据发布到 PHP 文件

[英]POSTing data via AJAX to a PHP file

我有一个接受多个文件的输入:

<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,则按照我在代码中的说明) )。

笔记:

  1. 我已经确保(使用其他 JS 代码) for循环中的propertyImages.files.length不能大于 30,并且当我附加 30 个图像时,长度确实是 30。

  2. 我已确保max_input_vars文件中的 max_input_vars 不是问题。

  3. 最后 10 个文件的图像大小不是 0。这已通过formData的日志记录得到证实

看看max_file_uploads指令:

max_file_uploads integer

允许同时上传的最大文件数。

如“文件上传”部分的表格所示,默认为 20:

Name              Default   Changeable       Changelog
...
max_file_uploads  20        PHP_INI_SYSTEM   Available since PHP 5.2.12.

您需要更新php.ini的值。

还要检查你没有超过post_max_sizeupload_max_filesize

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM