2011-12-12 85 views
3

我無法從$ _FILES中檢索我的php代碼中的多個文件。 這裏是輸入形式:

<form enctype="multipart/form-data" action="file-upload.php" method="POST"> 
    Upload the several files:<input type="file" multiple="multiple" name="uploaded" id="id_upload" /> 
    <input type="submit" value="Upload" /> 
</form> 

下面是從文件upload.php的PHP代碼:

// first let's find out how many files were uploaded.. 
$numUploadedfiles = count($_FILES['uploaded']); 
$num_FILES = count($_FILES); 
     // BOTH COUNTS ARE 5. I SELECT 7 FILE NAMES FOR UPLOADING THOUGH. 


echo "<br>" . "The number of uploaded files is == " . $numUploadedfiles; 
echo "<br>" . "Here is the name of _FILES['uploaded']: " . $_FILES['uploaded']; 
    // THE NAME REPORTED IS 'array' AND THE COUNT IS 5.. 


echo "<br>" . "The count size of _FILES is == " . $num_FILES; 
echo "<br>" . "Here is the name of _FILES => " . $_FILES; 
     // HERE ALSO, THE NAME REPORTED IS 'array' AND THE COUNT IS 5. 


echo "<br>file temp_name " . $i . " is: " . $_FILES['uploaded']['tmp_name']; 
echo "<br>file name " . $i . " is: " . $_FILES['uploaded']['name']; 
     // THE NAME REPORTED HERE IS THE FILENAME OF LAST OF THE 7 FILES I UPLOADED (not sure why.) 


echo "<br>" . "Here are the filenames: "; 
for($i = 0; $i < $numUploadedfiles; $i++) 
{ 
    echo "<br>filename " . $i . " is: " . $_FILES['uploaded'][$i]; 
} 
exit(); 

當我運行這就是,「for」循環開始時會發生什麼,一個錯誤消息表示$ i索引到數組_FILES ['uploaded'] [$ i]無效。

這是爲什麼?我需要獲得這7個文件名並能夠將它們保存在服務器上。我怎麼能:

1)獲得一個準確的「計數」的文件數量?當我上傳7個文件時,上面的代碼給出了5的計數

2)如何通過'for'循環中的_FILES數組正確索引? PHP告訴我$ i的值爲0,1,2,3 .... 無效。

(PS我使用的輸入類型=「文件」多個=「多個」名稱=「上傳」 ID =「id_upload」從我看到在Retrieving file names out of a multi-file upload control with javascript實現多文件上傳的示例代碼)

+1

看過這個嗎?可以幫忙(查看評論):http://verens.com/2009/12/28/multiple-file-uploads-using-html5/ –

+1

是的,這太可怕了!我不知道爲什麼很難找到有關它是如何工作的信息,以及爲什麼它非直觀地工作。我努力尋找那個信息。無論如何,很高興你現在能夠正常工作。 –

回答

5

你名稱應該是數組:

 
<input type="file" name="uploaded[]" id="id_upload" /> 

+0

好吧 - 不過現在當我檢查$ uploaded_size = $ _FILES ['uploaded'] ['size'];價值超過70,000,000!爲什麼? – SandHawkerTech

+0

感謝Sudhir,這是讓我的代碼正常工作的'第一步'。第二步是基督徒提到上面評論中的文章。 – SandHawkerTech

0

此代碼沒有實現您的目標。你應該使用幾個輸入標籤。

如果你想上傳幾個文件但文件數量是可變的,你可以使用java腳本獲取文件數