获取详细信息选择图像在PHP中

时间:2013-07-12 19:55:12

标签: php html5 performance multi-upload

我正在使用html5 js to upload multiple images。为什么当我们上传多个图像时,结果(PHP:print_r($_FILES['upimg']['name']))仅显示最后选择的图像的详细信息?

例如:(我选择了以下图像名称)

  

111111.gif

     

222222.gif

     

333333.gif

PHP代码最后选择的图片显示:333333.gif

但我希望显示所有图像的细节。

我该怎么办?

DEMO:http://codepad.viper-7.com/16abeG

完整代码:

<!doctype html>
<html>
<head>
    <?php
if($_FILES){
    echo '<pre>';
    print_r($_FILES['upimg']['name']);

        //for($i=0; $i<count($_FILES['name']); $i++){
            //if ($_FILES['error'][$i] == 0) {
                ////do your stuff here, each image is at $_FILES['tmp_name'][$i]
            //}
        //}
    }
?>
  <title>file input click() demo</title>
  <script type="text/javascript">
    function doClick() {
      var el = document.getElementById("fileElem");
      if (el) {
        el.click();
      }
    }
    function handleFiles(files) {
      var d = document.getElementById("fileList");
      if (!files.length) {
        d.innerHTML = "<p>No files selected!</p>";
      } else {
        var list = document.createElement("ul");
        d.appendChild(list);
        for (var i=0; i < files.length; i++) {
          var li = document.createElement("li");
          list.appendChild(li);

          var img = document.createElement("img");
          img.src = window.URL.createObjectURL(files[i]);;
          img.height = 60;
          img.onload = function() {
            window.URL.revokeObjectURL(this.src);
          }
          li.appendChild(img);

          var info = document.createElement("span");
          info.innerHTML = files[i].name + ": " + files[i].size + " bytes";
          li.appendChild(info);
        }
      }
    }
  </script>
</head>
<body>
  <p>This is a demo of calling <code>click()</code> on a form's file picker.
  Note that the file input element is actually hidden here, so the
  user doesn't have to see the path to the selected file.</p>
      <a href="javascript:doClick()">Select some files</a>
  <div id="fileList">
    <p>No files selected!</p>
  </div>
  <form action="#" method="post" enctype="multipart/form-data">
    <input name="upimg[]" type="file" id="fileElem" multiple accept="image/*" style="display:none" onchange="handleFiles(this.files)">
        <input type="submit" value="Click to see the result">
  </form>
</body>
</html>

3 个答案:

答案 0 :(得分:0)

我认为您使用HTML 5的逻辑是错误的,但是不能通过AJAX上传图像,而是使用此方法的表单。

Miltiple只有HTML 5,没有AJAX就不能用于PHP目的。

我创建了类似的插件,您可以查看:https://github.com/dimitardanailov/js-control-files-uploader

答案 1 :(得分:0)

尝试将它包装在for循环中,有点像你注释掉的代码......

if(isset($_FILES["upimg"]["name"])) {
    for($i=0; $i<count($_FILES["upimg"]["name"]);$i++) {
        echo $_FILES["upimg"]["name"][$i];
    }
}

答案 2 :(得分:0)

为什么不在任何.parent div之前使用
标签?

对于宽度比你当然可以使用jquery来做。

myElement.width = (parentElement.width/100)*40