我無法從$ _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實現多文件上傳的示例代碼)
看過這個嗎?可以幫忙(查看評論):http://verens.com/2009/12/28/multiple-file-uploads-using-html5/ –
是的,這太可怕了!我不知道爲什麼很難找到有關它是如何工作的信息,以及爲什麼它非直觀地工作。我努力尋找那個信息。無論如何,很高興你現在能夠正常工作。 –