2012-04-19 78 views
0

我有一個文件輸入是一個數組,但我以前不再工作,它似乎爲多個文件拋出一個錯誤,它只是上傳最新的文件,我不能指望我們什麼我做錯了:(上傳多個文件數組錯誤

<input type="file" name="userfile[]" id="userfile" class="multi" /> 

然後在我的控制器:

$upload_files = $_FILES; 
var_dump($upload_files['userfile']['name']); 
// get the selected files out of the array 
for($i = 0; $i < count($upload_files['userfile']['name']); $i++) { 
    $_FILES['userfile'] = array(
     'name' => $upload_files['userfile']['name'][$i], 
     'type' => $upload_files['userfile']['type'][$i], 
     'tmp_name' => $upload_files['userfile']['tmp_name'][$i], 
     'error' => $upload_files['userfile']['error'][$i], 
     'size' => $upload_files['userfile']['size'][$i] 
); 
+1

我相信文件的陣列西港島線是'$ _FILES [ 'userfile的' ]'not'$ _FILES ['userfile'] ['name']' – Dale 2012-04-19 12:05:50

+1

所以在你的情況下:'$ upload_files ['userfile']' – Dale 2012-04-19 12:06:54

+0

試試這個鏈接 http://www.codeproject.com/Articles/ 19606/Javascript的PHP-多文件-U pload – chhameed 2012-04-19 12:18:08

回答

2

你覆蓋在陣列的每個迭代$_FILES['userfile'](這就是爲什麼只有最後一個文件上傳)

每次循環時創建一個新的數組元素。

此外,改變你的遍歷$upload_files['userfile'],而不是$upload_files['userfile']['name']迭代在上述意見提出:)


只是一個指針 - 這是更好地分配count($upload_files['userfile'])一個變量您for循環之外。如果你的數組只有少數幾個元素,那麼它可能沒有太大的區別,但是在使用大型數組時,它有很大的性能優勢。

退房http://www.phpbench.com/

1

你重寫陣列在每次迭代,你可以試試:

foreach($upload_files['userfile'] as $i => $file) { 
    $_FILES['userfile_org'][$i] = array(
     'name' => $file['name'], 
     'type' => $file['type'], 
     'tmp_name' => $file['tmp_name'], 
     'error' => $file['error'], 
     'size' => $file['size'] 
    ); 
    } 
    echo "<pre>"; var_dump($_FILES['userfile_org']); die; 
+0

你的ans和deifud ans有什麼區別? – chhameed 2012-04-19 12:22:04

+0

唯一的區別是我有樣品解決方案。雖然站在沒有看到他的迴應:( – 2012-04-19 12:38:36