7

我有以下代码可以工作和上传,但它不会循环通过数组来上传每个文件,只是第一个文件。

<form method="post" enctype="multipart/form-data" action="http://<?php echo $pageURL;?>">   
<input class="new" multiple="multiple" name="documents[]" type="file" />
<input class="new" multiple="multiple" name="documents[]" type="file" />
<input type="submit" class="button" name="addMaterials" value="Add" />

<?php

    foreach($_FILES['documents']['tmp_name'] as $key => $tmp_name)
        {
            $file_name = $key.$_FILES['documents']['name'][$key];
            $file_size =$_FILES['documents']['size'][$key];
            $file_tmp =$_FILES['documents']['tmp_name'][$key];
            $file_type=$_FILES['documents']['type'][$key];  
            move_uploaded_file($file_tmp,"files/".time().$file_name);
        }
?>

我需要它来循环浏览我的文档 [] 文件数组。

文档数组的示例print_r()

Array ( 
    [name] => Array ( [0] => AcroRd32.exe ) 
    [type] => Array ( [0] => application/x-msdownload ) 
    [tmp_name] => Array ( [0] => C:\xampp\tmp\phpE8BD.tmp ) 
    [error] => Array ( [0] => 0 ) 
    [size] => Array ( [0] => 1343112 ) 
    )

任何帮助表示赞赏。

4

6 回答 6

18

您可以使用我更新的代码,根据我的演示,它非常适合多个文件上传

 <?php
if(isset($_FILES['documents'])){

foreach($_FILES['documents']['tmp_name'] as $key => $tmp_name)
{
    $file_name = $key.$_FILES['documents']['name'][$key];
    $file_size =$_FILES['documents']['size'][$key];
    $file_tmp =$_FILES['documents']['tmp_name'][$key];
    $file_type=$_FILES['documents']['type'][$key];  
    move_uploaded_file($file_tmp,"galleries/".time().$file_name);
}
}else{
echo "<form enctype='multipart/form-data' action='test1.php' method='POST'>";
 echo "File:<input name='documents[]' multiple='multiple' type='file'/><input type='submit' value='Upload'/>";

 echo "</form>";
}
?>
于 2013-04-11T06:17:48.627 回答
3

对于任何尝试使用单个文件 php 函数(我正在使用类,但您可以更改为函数)的人:

html:

                        <input type="file" name="foto[]" />
                        <input type="file" name="foto[]" />
                        <input type="file" name="foto[]" />
                        <input type="file" name="foto[]" />
                        <input type="file" name="foto[]" />

php:

if (isset($_FILES['foto'])) {

      $arquivo = array();
    foreach ($_FILES['foto']["name"] as $file=>$key) {

                    // the empty input files create an array index too, so we need to
                    // check if the name exits. It means the file exists.
        if (!empty($_FILES['foto']["name"][$file])) {
          $arquivo ["name"] = $_FILES['foto']["name"][$file];
          $arquivo ["type"] = $_FILES['foto']["type"][$file];
          $arquivo ["tmp_name"] = $_FILES['foto']["tmp_name"][$file];
          $arquivo ["error"] = $_FILES['foto']["error"][$file];
          $arquivo ["size"] = $_FILES['foto']["size"][$file];

$foto = new foto(); // create an obj foto
    // $arquivo means file, it`s our file format as a single $_file['file']
if ($foto -> upload($arquivo)) { // if its uploaded than save
    $foto -> save();
}


    }

    } 

}

在我的照片课上:

public function upload($foto) {

    $upload_dir = "D:/xampp/htdocs/prova/fotos/";
    $file_dir = $upload_dir . $foto["name"];

    $move = move_uploaded_file($foto["tmp_name"], $file_dir);
    $this -> arquivo = $foto["name"]; // use this to save to db later

    // this serves to return true if the file is uploaded
    $retorno = ($move) ? 1 : 0; 
    return $retorno;

}
于 2013-11-25T18:05:06.947 回答
1

尝试使用此代码进行多文件上传

<form method="post" action="upload-page.php" enctype="multipart/form-data">
 <input name="filesToUpload[]" id="filesToUpload" type="file" multiple="" />
</form>

在 PHP 中

if(count($_FILES['uploads']['filesToUpload'])) {
foreach ($_FILES['uploads']['filesToUpload'] as $file) {

    //do your upload stuff here
    echo $file;

}
}

使用 javascript 显示文件名

//get the input and UL list
var input = document.getElementById('filesToUpload');
var list = document.getElementById('fileList');

//empty list for now...
while (list.hasChildNodes()) {
list.removeChild(ul.firstChild);
}

//for every file...
for (var x = 0; x < input.files.length; x++) {
//add to list
var li = document.createElement('li');
li.innerHTML = 'File ' + (x + 1) + ':  ' + input.files[x].name;
list.append(li);
}
于 2013-04-11T06:10:24.463 回答
0

试试这种循环方式documents array()

<?php 

foreach($_FILES['documents']['tmp_name'] as $key => $tmpName) {

  $file_name = $_FILES['documents']['name'][$key];
  $file_type = $_FILES['documents']['type'][$key];
  $file_size = $_FILES['documents']['size'][$key];
  $file_tmp  = $_FILES['documents']['tmp_name'][$key];

  move_uploaded_file($file_tmp,"files/".time().$file_name);
}

?>
于 2013-04-11T06:04:48.250 回答
0

使用多个输入字段上传文件。

HTML

<form action="" method="post" enctype="multipart/form-data">
          <p><input type="file" name="file_array[]"></p>
          <p><input type="file" name="file_array[]"></p>
          <p><input type="file" name="file_array[]"></p>
          <input type="submit" value="Upload all files">
        </form>

PHP

<?php
if(isset($_FILES['file_array'])){
    $name_array = $_FILES['file_array']['name'];
    $tmp_name_array = $_FILES['file_array']['tmp_name'];
    $type_array = $_FILES['file_array']['type'];
    $size_array = $_FILES['file_array']['size'];
    $error_array = $_FILES['file_array']['error'];
    for($i = 0; $i < count($tmp_name_array); $i++){
        if(move_uploaded_file($tmp_name_array[$i], "test_uploads/".$name_array[$i])){
            echo $name_array[$i]." upload is complete<br>";
        } else {
            echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
        }
    }
}
?>
于 2018-01-04T19:38:09.747 回答
0

上传多个文件很容易,请按照以下步骤操作。

  1. 使用数组表示法意味着方括号与输入的名称像这样
  2. <input type="file" id="indtbl_logo[]" name="indtbl_logo[]" multiple />
  3. 您可以使用$_FILES全局变量循环遍历它
    foreach ($_FILES['indtbl_logo']['tmp_name'] as $key => $tmp_name) {
        $file_name = $key . $_FILES['indtbl_logo']['name'][$key];
        $file_size = $_FILES['indtbl_logo']['size'][$key];
        $file_tmp = $_FILES['indtbl_logo']['tmp_name'][$key];
        $file_type = $_FILES['indtbl_logo']['type'][$key]; 

        echo $file_name;
        echo "<br>"; 
    }
于 2020-05-29T10:44:20.700 回答