【问题标题】:Can't upload multiple files with different extension?不能上传多个不同扩展名的文件?
【发布时间】:2018-02-19 09:44:32
【问题描述】:

如果所有文件的扩展名不同,则无法上传多个文件!如果我选择了两个 png 文件,它可以工作。但是选择两个不同的文件扩展名 (png,pdf)$_FILES 中得到了空数组!

index.php

<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js" > </script>
</head>

<body>
  <form>
      <input name="file[]" type="file" multiple/>
      <input type="button" value="Upload" />
  </form>
  <progress></progress>
  <script>
$(':button').on('click', function() {
$.ajax({
  // Your server script to process the upload
  url: 'upload.php',
  type: 'POST',

  // Form data
  data: new FormData($('form')[0]),

  // Tell jQuery not to process data or worry about content-type
  // You *must* include these options!
  cache: false,
  contentType: false,
  processData: false,

  // Custom XMLHttpRequest
  xhr: function() {
      var myXhr = $.ajaxSettings.xhr();
      if (myXhr.upload) {
          // For handling the progress of the upload
          myXhr.upload.addEventListener('progress', function(e) {
              if (e.lengthComputable) {
                  $('progress').attr({
                      value: e.loaded,
                      max: e.total,
                  });
              }
          } , false);
      }
      return myXhr;
  },
});
});
  </script>
</body>
</html>

上传.php

<?php var_dump($_FILES); ?>

结果图片

【问题讨论】:

    标签: php jquery ajax


    【解决方案1】:

    希望能帮到你。

    demo.php

    <?php
      if(isset($_FILES)&&!empty($_FILES)){
          for($i=0;$i<count($_FILES);$i++){
              echo "File ".($i+1)." is ".$_FILES["file-".$i]['name']."\n";
          }
          die;
      }
    ?>
    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
    // Updated part
    jQuery.each(jQuery('#file')[0].files, function(i, file) {
        data.append('file-'+i, file);
    });
    
    // Full Ajax request
    $(".update").click(function(e) {
        // Stops the form from reloading
        e.preventDefault();
    
            $.ajax({
            url: 'demo.php',
            type: 'POST',
            contentType:false,
            processData: false,
            data: function(){
                var data = new FormData();
                jQuery.each(jQuery('#file')[0].files, function(i, file) {
                    data.append('file-'+i, file);
                });
                return data;
            }(),
                success: function(result) {
                alert(result);
                },
            error: function(xhr, result, errorThrown){
                alert('Request failed.');
            }
            });
    });
    });
    </script>
    </head>
    <body>
    <form enctype="multipart/form-data" method="post">
      <input id="file" name="file[]" type="file"  multiple/>
      <input class="update" type="submit" />
    </form>
    <body>
    </html>
    

    【讨论】:

    • 我的问题是但是选择两个不同的文件扩展名(png,pdf)在$_FILES中得到了空数组
    • 我运行这段代码并选择了两个不同的文件扩展名 (png,pdf) 但不是 $_FILES 中的空数组
    • 真的!!它发生在我的身上。
    • 可能是因为你用的是https,用http重试。可能从 http 重定向到 https 时 $_FILES 为空。或编辑代码行 26 url: 'https:///demo.php'.
    • 可能重复重定向 https 您检查 .htaccess 或配置您的服务器。
    【解决方案2】:

    我认为您可以使用以下代码:-

    <button id="upload">Upload</button>
        <script type="text/javascript">
                    $(document).ready(function (e) {
                        $('#upload').on('click', function () {
                            var form_data = new FormData();
                            var ins = document.getElementById('multiFiles').files.length;
                            for (var x = 0; x < ins; x++) {
                                form_data.append("files[]", document.getElementById('multiFiles').files[x]);
                            }
                            $.ajax({
                                url: 'uploads.php', // point to server-side PHP script 
                                dataType: 'text', // what to expect back from the PHP script
                                cache: false,
                                contentType: false,
                                processData: false,
                                data: form_data,
                                type: 'post',
                                success: function (response) {
                                    $('#msg').html(response); // display success response from the PHP script
                                },
                                error: function (response) {
                                    $('#msg').html(response); // display error response from the PHP script
                                }
                            });
                        });
                    });
                </script>
    

    【讨论】:

    • 你能告诉我你遇到了什么错误吗?并在表单标签 enctype="multipart/form-data" 中传递 thi 属性
    • 刚刚得到空数组!
    • 我的问题是但是选择两个不同的文件扩展名(png,pdf)在$_FILES中得到了空数组
    • 我将您的代码粘贴到我的本地服务器中,它运行良好。为什么 test.php 加载 3 次?
    猜你喜欢
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-02
    相关资源
    最近更新 更多