简体   繁体   English

如何上传文件数组?

[英]How to upload an array of files?

I'm trying to build an array of files by submitting a form several times, then move those files to a directory, but it's not working. 我试图通过多次提交表单来构建一个文件数组,然后将这些文件移动到一个目录,但它不起作用。 Every uploads just overrides the previous and then it doesn't even move that one (the upload_to_file() function doesn't do anything) 每个上传只是覆盖前一个,然后它甚至不移动那个(upload_to_file()函数不做任何事情)

HTML: HTML:

<form id="form" action="home.php" method="post" enctype="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="8000000">
        <input class="upload_btn" type="file" name="images[]" id="image_file">
        <input type="submit" id="img_submit" class="form_button" name="submit_image" value="upload"/>
</form>

It is important that there is only one upload button which can be used to upload many files. 重要的是只有一个上传按钮可用于上传许多文件。

I need them to be stored in an array so I can display their ['name'] anywhere with a loop. 我需要将它们存储在一个数组中,这样我就可以在任何地方用循环显示它们的['name']。 for ($i = 0; $i < count($_FILES['images']['name']); $i++){}

Then once another form is submitted it calls a function to move each file in the array to a directory. 然后,一旦提交了另一个表单,它就会调用一个函数将数组中的每个文件移动到一个目录中。

Function inside an included php file: 包含的php文件中的函数:

function upload_to_file(){
$image_paths = array();
$target_dir = "uploads/images/";
$path = $target_dir . basename($_FILES['images']['name'][0]);

if(isset($_FILES['images']['name'][0]) && $_FILES['images']['size'][0] > 0)
{
    if (move_uploaded_file($_FILES['images']['tmp_name'][0], $path)) {
        $image_paths[0] = "uploads/images/";
    }
}
return $image_paths;

}

I'm only testing it with the first element in the array, but will need to make a loop later. 我只是用数组中的第一个元素测试它,但是稍后需要进行循环。

Here is the program, which help you to upload multiple files. 这是程序,它可以帮助您上传多个文件。 in the code "sub" is the submit button name. 在代码“sub”中是提交按钮名称。 "upload" is the name of file controller, the uploaded files are stored in the directory called "img" that you have to create on your root folder. “upload”是文件控制器的名称,上传的文件存储在您必须在根文件夹上创建的名为“img”的目录中。

<?php

if (isset($_POST['sub'])) {
    if (count($_FILES['upload']['name']) > 0) {
     for ($i=0; $i<count($_FILES['upload']['name']); $i++) {
      $tmpFilePath = $_FILES['upload']['tmp_name'][$i];
      if ($tmpFilePath != "") {
        $shortname = $_FILES['upload']['name'][$i];
        $filePath = "img/" . date('d-m-Y-H-i-s').'-'.$_FILES['upload']['name'][$i];
        if (move_uploaded_file($tmpFilePath, $filePath)) {
          $files[] = $shortname;                    
        }
      }
    }
  }
  echo "<h1>Uploaded:</h1>";    
  if(is_array($files)){
    echo "<ul>";
    foreach($files as $file){
      echo "<li>$file</li>";
    }
    echo "</ul>";
  }
}
?>

HTML Code is given 给出了HTML代码

<form action="" enctype="multipart/form-data" method="post">
    <input id='upload' name="upload[]" type="file" multiple="multiple" />
    <input type="submit" name="sub" value="Upload Now">
</form>

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

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