如何通过ajax以与纯PHP相同的格式获取$ _FILES数据

问题描述 投票:0回答:1

我正在上传脚本。我需要编辑的只是$ _FILES数组的格式。

我的HTML

<input id="upload_dialog" name="file[]" type="file" multiple>

我的Javascript

<script>
    $('#upload_dialog').on('change', function(event) {
      var form = new FormData();
      $.each(event.target.files, function(key, value) {
        form.append(key, value);
      });

      $.ajax({
        url: 'url,
        type: 'POST',
        data: form,
        processData: false,
        contentType: false,
        cache: false,
        async: true,
        done: function(data) {
          data = JSON.parse(data);

      });
    });
</script>

这个数组我将通过ajax上传文件。

array(2) {
  [0]=>
  array(5) {
    ["name"]=>
    string(11) "ps-logo.jpg"
    ["type"]=>
    string(10) "image/jpeg"
    ["tmp_name"]=>
    string(23) "/home/www/tmp/phpMBu4TE"
    ["error"]=>
    int(0)
    ["size"]=>
    int(24722)
  }
  [1]=>
  array(5) {
    ["name"]=>
    string(12) "tnk-logo.png"
    ["type"]=>
    string(9) "image/png"
    ["tmp_name"]=>
    string(23) "/home/www/tmp/php9yPGpf"
    ["error"]=>
    int(0)
    ["size"]=>
    int(23748)
  }
}

但我需要这种类型的数组,如果我只通过纯PHP提交上传表单,我通常会得到这种数据。

array(1) {
  ["file"]=>
  array(5) {
    ["name"]=>
    array(2) {
      [0]=>
      string(12) "tnk-logo.png"
      [1]=>
      string(11) "ps-logo.jpg"
    }
    ["type"]=>
    array(2) {
      [0]=>
      string(9) "image/png"
      [1]=>
      string(10) "image/jpeg"
    }
    ["tmp_name"]=>
    array(2) {
      [0]=>
      string(23) "/home/www/tmp/phpWGezym"
      [1]=>
      string(23) "/home/www/tmp/phpIqOpKY"
    }
    ["error"]=>
    array(2) {
      [0]=>
      int(0)
      [1]=>
      int(0)
    }
    ["size"]=>
    array(2) {
      [0]=>
      int(23748)
      [1]=>
      int(24722)
    }
  }
}

有没有简单的方法如何通过ajax发送数据来获得这种类型的数组?

javascript php jquery ajax upload
1个回答
0
投票

JavaScript解决方案

不要将文件作为新表单的元素添加,而是使用输入已在其中的表单创建FormData实例。

$('#upload_dialog').on('change', function(event) {
    var input = event.target,
        form = input.form,
        data = new FormData(form);

    $.ajax({
        url: '/form.php',
        type: 'POST',
        data: data,
        processData: false,
        contentType: false,
        cache: false,
        async: true,
        done: function(data) {
            data = JSON.parse(data);
        }
    });
});

我原来不那么好的PHP解决方案

要让PHP处理文件可能提交的两种方式,您可以执行类似这样的操作,将其转换为您想要的格式(如果它尚未采用该格式):

function maybe_ajax_files($field) {
    // If $_FILES is empty or it's already got the field we need,
    // just use it as-is.
    if (empty($_FILES) || isset($_FILES[$field])) {
        return $_FILES;
    }

    // Otherwise, regroup them...
    $regrouped = array();
    foreach ($_FILES as $file) {
        foreach ($file as $key => $val) {
            if (!isset($regrouped[$key])) {
                $regrouped[$key] = array();
            }
            $regrouped[$key][] = $val;
        }
    }

    // Then assign that array to the desired field in a new array.
    $files = array();
    $files[$field] = $regrouped;
    return $files;
}

// 'file' is the name of the field in your example:
// <input id="upload_dialog" name="file[]" type="file" multiple>
$files = maybe_ajax_files('file');
© www.soinside.com 2019 - 2024. All rights reserved.