我无法从$ _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>
Run Code Online (Sandbox Code Playgroud)
这是来自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();
Run Code Online (Sandbox Code Playgroud)
当我运行它时会发生什么,当'for'循环开始时,一条错误消息说$ i索引到数组_FILES ['uploaded'] [$ i]无效.
这是为什么?我需要获取这7个文件名,并能够将它们保存在服务器上.我怎么能够:
1)获得文件数量的准确"计数"?当我上传7个文件时,上面的代码给出了5的计数
2)如何在'for'循环中正确索引_FILES数组?PHP告诉我0,1,2,3 ......的$ i值无效.
(PS我使用输入类型="文件"倍="多个"name ="上传"id ="id_upload"代码我看到的示例用于启用多文件上传从多文件上传控件中检索文件名用javascript)