繁体   English   中英

如何上传文件夹中的所有图片

[英]how to upload all images from a folder

尝试从文件夹上传所有图像
服务器端的目标文件夹是test
图像的目标格式是uniqid().jpg

问题 - 服务器端的结果数组似乎是空的

<input type='file' class='inpfile' id='inpfo' webkitdirectory='' directory='' multiple='true'>  

var inpfo = $('#inpfo');

inpfo.on('change', function(){
    var flist = inpfo[0].files;
    var fd = new FormData();
    fd.append('flist', flist);
    $.ajax({
    url: 'upload.php',
    type: 'POST',
    data: fd,
    async: false,
    contentType: 'multipart/form-data',
    success: function (data){
      console.log(data);  // Undefined array key "flist"
    },
    cache: false,
    contentType: false,
    processData: false
  });
});

上传.php

$arr = $_FILES['flist'];  //seems this is an empty array
foreach($arr as $el){
    $tempname = $el['tmp_name'];
    $uniq = uniqid();
    $img = imagecreatefromjpeg($tempname);
    $targ = 'test/' . $uniq . '.jpg';
    imagejpeg($img, $targ);
}

您正在传递一个不正确的 FileList 对象fd.append您必须遍历列表并单独添加每个文件。

inpfo.on('change', function(){
    var flist = inpfo[0].files;
    var fd = new FormData();
    for (let i = 0; i < flist.length; i++){ 
        fd.append('flist[]', flist[i]); // add [] to denote array
    }
    $.ajax({
    url: 'upload.php',
    type: 'POST',
    data: fd,
    async: false,
    success: function (data){
      console.log(data);  // Undefined array key "flist"
    },
    cache: false,
    contentType: false,
    processData: false
  });
});

您使用的文件数组不正确,请参见here

$arr = $_FILES['flist'];   
foreach($arr['tmp_name'] as $tempname){
    $uniq = uniqid();
    $img = imagecreatefromjpeg($tempname);
    $targ = 'test/' . $uniq . '.jpg';
    imagejpeg($img, $targ);
}

暂无
暂无

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

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