PHP中的多个文件的问题

时间:2013-01-08 10:36:21

标签: php ajax html5 file-upload

首先,我很抱歉我的英语不好。

我想使用HTML5 + JavaScript + PHP

将多个文件上传到服务器

用HTML和JS做就好像要模拟一样。正确操作。

<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
  .thumb {
    height: 75px;
    border: 1px solid #000;
    margin: 10px 5px 0 0;
  }
</style>
</head>

<body>

<form action="lanzar.php" enctype="multipart/form-data" method="post"  >
<input type="file" id="files"  name="files[]" multiple />
<input type="submit" />
<input type="reset"/>
<output id="list"></output>
</form>

<script>
  function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object

    // Loop through the FileList and render image files as thumbnails.
    for (var i = 0, f; f = files[i]; i++) {

      // Only process image files.
      if (!f.type.match('image.*')) {
        continue;
      }

      var reader = new FileReader();

      // Closure to capture the file information.
      reader.onload = (function(theFile) {
      return function(e) {
      // Render thumbnail.
      var span = document.createElement('span');
          span.innerHTML = ['<img class="thumb" src="', e.target.result,'" title="', escape(theFile.name), '"/>'].join('');
          document.getElementById('list').insertBefore(span, null);
        };
      })(f);

      // Read in the image file as a data URL.
      reader.readAsDataURL(f);
    }
  }

  document.getElementById('files').addEventListener('change', handleFileSelect, false);
</script>
</body>
</html>

然后我有一个简单的PHP代码,可以显示图像。我的问题是,只有最后一个文件被选中。案件不休息。就像数组不是制作数据的正确方法一样。

if(isset($_FILES['files']['tmp_name'])){
    echo $num_files = count($_FILES['files']['tmp_name']);

    foreach($_FILES['files']['name'] as $key =>$value){

        is_uploaded_file($_FILES['files']['tmp_name'][$key]);

        $origen = $_FILES['files']['tmp_name'][$key];
        $destino = "img/".$_FILES['files']['name'][$key];
        move_uploaded_file($origen, $destino);
    }
}

我做得不好? 感谢

1 个答案:

答案 0 :(得分:0)

我认为您的脚本运行正常,因为我在本地服务器上测试过,所有图片都正在上传。请再次检查。