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

标签 javascript php ajax

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

<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. 我已确定 PHP.ini 文件中的 max_input_vars 不是问题所在。

  3. 最后 10 个文件的图像大小不是 0。这已通过 formData

    的日志记录确认

最佳答案

看看 max_file_uploads指令:

max_file_uploads integer

The maximum number of files allowed to be uploaded simultaneously.

如“文件上传”部分的表格所示,默认为 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 .

关于javascript - 通过 AJAX 将数据发布到 PHP 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58641932/

相关文章:

javascript - 选择对象时更改链接的颜色

javascript - 单击按钮时显示数据表表中的所有记录

javascript - 当 anchor 击顶部屏幕时添加类

php 更新查询不会使用 pdo 进行更新

php - 防止 AJAX 聊天显示重复消息(Javascript、PHP、MySQL)

javascript - 为什么我们需要将 window 和 undefined 传递给这个 jquery 插件?

php - laravel 如何获取除一个模型之外的所有模型

使用 CORS 重定向的 Ajax 请求在 IE11 中失败

javascript - 使用ajax上传多个文件

javascript - AJAX 调用带有参数的函数