2013-04-11 51 views
4

我有以下代碼工作和上傳,但它不會循環通過數組上傳每個文件,只是第一個文件。PHP多文件陣列

<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) 
    ) 

任何幫助理解。

+0

你能告訴我們的HTML文件上傳域? – Dracs 2013-04-11 05:52:23

+0

哪一個數組會是這樣的? '$ key。$ _ FILES ['...' – 2013-04-11 05:53:21

+0

html添加,我需要做的就是上傳每個文件。 – 2013-04-11 05:54:19

回答

12

你可以使用我更新的代碼,並按照我的演示,它正在完美的多文件上傳

<?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>"; 
} 
?> 
+0

這仍然只是循環一次! – 2013-04-11 06:50:28

+1

哎呀,在我的其他代碼錯誤 - 這個工程! – 2013-04-11 06:51:26

+0

如果文件值是數組newfiles [] [0],newfiles [] [1]的數組? – 2016-01-05 13:01:56

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); 
} 

?> 
+0

似乎數組裏只有一個文件,所以我不確定爲什麼我的文件沒有添加到數組之前,我執行此代碼。 – 2013-04-11 06:07:32

+0

我將編輯代碼。 – 2013-04-11 06:09:10

+0

現在我編輯了我的答案。請檢查一下。 – Ranjith 2013-04-11 06:22:06

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); 
} 
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; 

} 
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>"; 
     } 
    } 
} 
?>