我无法从$ _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>
这是来自file-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我使用输入类型=“文件”multiple =“multiple”name =“uploaded”id =“id_upload”代码来自我看到的用于在Retrieving file names out of a multi-file upload control with javascript启用多个文件上传的示例
答案 0 :(得分:5)
你的名字应该是数组:
<input type="file" name="uploaded[]" id="id_upload" />
答案 1 :(得分:0)
此代码未实现您的目标。你应该使用几个输入标签。
如果您想上传多个文件但文件数量是可变的,您可以使用java脚本获取文件数量