【问题标题】:multiple file uploader, sends only one file多文件上传器,只发送一个文件
【发布时间】:2013-01-09 03:05:23
【问题描述】:

我已经尝试了将近两天,但它不起作用,我有一个简单的输入文件(在 php 中)

echo '<input id="files" type="file" name="ufile[]" multiple="multiple"/>';

假设同时发送多个文件到另一个页面,我接收文件的代码如下:

for($i=0; $i<count($_FILES['ufile']['name']); $i++) {
  //Get the temp file path
  $tmpFilePath = $_FILES['ufile']['tmp_name'][$i];

  //Make sure we have a filepath
  if ($tmpFilePath != ""){
    //Setup our new file path
    $newFilePath = 'upload/'. $_FILES['ufile']['name'][$i];

    //Upload the file into the temp dir
    if(move_uploaded_file($tmpFilePath, $newFilePath)) {

      //Handle other code here

    }
  }
}

但它只能接收一个文件,我也尝试使用复制,它给出了相同的结果,我尝试使用计数方法 print_r 和 var_dump 来计算文件数:

$a=count($_FILES['ufile']['name']);
print_r($a);
var_dump($a);

而且它们也都只显示文件。关于浏览器兼容性,我已经在少数浏览器上尝试过,包括最新版本的 Firefox、chrome 和 ...

提前致谢 我编辑这篇文章,包括输出 (var_dump) 并添加 html 表单 下面是 javascript 代码,在其中预览我使用输入文件选择的图像。 window.onload = function(){

    if(window.File && window.FileList && window.FileReader)
    {
        var filesInput = document.getElementById("files");

        filesInput.addEventListener("change", function(event){

            var files = event.target.files; //FileList object
            var output = document.getElementById("result");

            for(var i = 0; i< files.length; i++)
            {
                var file = files[i];

                if(!file.type.match('image'))
                  continue;

                var picReader = new FileReader();

                picReader.addEventListener("load",function(event){

                    var picFile = event.target;

                    var div = document.createElement("div");

                    div.innerHTML = "<img class='thumbnail' src='" + picFile.result + "'" +
                            "title='" + picFile.name + "'/>";

                    output.insertBefore(div,null);            

                });

                picReader.readAsDataURL(file);
            }                               

        });
    }
    else
    {
        console.log("not supported");
    }
}

    </script>

这是我的 html 表单:

echo '<form action="Data.php" method="post" enctype="multipart/form-data" name="form1" id="form1">';


echo'
<label for="files">Add image: </label>';

echo    '<input id="files" type="file" name="ufile[]" multiple/>';
    echo '
    <output id="result" />';

echo '<input type="submit" name="submit" value="Submit"id="newbutton" style="width:100px;height:30px" />';


</form> 

最后是 vardump 的结果

Array ( [ufile] => Array ( [name] => Array ( [0] => Awesoem-Arrow-Facebook-Timeline-Cover_02-@-GenCept.jpg ) [type] => Array ( [0] => image/jpeg ) [tmp_name] => Array ( [0] => C:\xampp\tmp\php6BDF.tmp ) [error] => Array ( [0] => 0 ) [size] => Array ( [0] => 120664 ) ) ) 

【问题讨论】:

  • 你能显示你最后一段代码的输出吗? print_r($a)
  • 您能显示您的表单的完整 HTML 吗?
  • 我也想看看 print_r($_FILES); 的输出当您尝试上传一个或多个文件时。
  • Array ([ufile] => Array ([name] => Array ([0] => Awesoem-Arrow-Facebook-Timeline-Cover_02-@-GenCept.jpg) [type] =>数组 ( [0] => image/jpeg ) [tmp_name] => 数组 ( [0] => C:\xampp\tmp\php6BDF.tmp ) [错误] => 数组 ( [0] => 0 ) [大小] => 数组 ( [0] => 120664 ) ) )
  • @DeeDee@Nicarus,我已经更新了我的帖子并添加了你所要求的一切!

标签: php html


【解决方案1】:

这里,精简到本质,是我过去在 PHP 中处理相同任务的方式:

if (isset($_FILES['name'])){
    $maxFiles = 4; 
    $counter = 0;
         while (is_uploaded_file($_FILES['name']['tmp_name'][$counter])){
              /*
                      do your magic with each file here
                  */
                  //remove the temp file after you're done with it
                  unlink ($_FILES['name']['tmp_name'][$counter]);
                  $counter++;
            }
}

因此,您无需计算预先上传的文件总数,而是继续遍历 files 数组,直到没有更多文件为止。如果这对你不起作用,请告诉我。

【讨论】:

    【解决方案2】:

    一个“文件”类型的输入字段只能包含一个文件。您只使用一个输入字段吗?

    【讨论】:

    • 你的意思是即使它被定义为 ufile[] 和多个?
    • 对不起,我弄糊涂了,你使用了multiple属性
    • 在你的最后一个代码中你使用'',试试''
    • 什么浏览器?什么版本?你认为上面的javascript可能有,我不知道对它有什么影响?
    猜你喜欢
    • 2012-12-10
    • 2013-06-18
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    • 2014-01-02
    • 2018-10-27
    • 1970-01-01
    • 2019-01-11
    相关资源
    最近更新 更多