gpt4 book ai didi

php - 无法从 php 中的 $_FILES 获取多个上传的文件

转载 作者:可可西里 更新时间:2023-11-01 00:35:42 25 4
gpt4 key购买 nike

我无法从 $_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”循环开始时,一条错误消息指出数组 _FILES['uploaded'][$i] 中的 $i 索引无效。

这是为什么呢?我需要获取这 7 个文件名并能够将它们保存在服务器上。我怎样才能:

1) 获得文件数量的准确“计数”?当我上传 7 个文件时,上面的代码计数为 5

2) 如何在“for”循环中正确索引 _FILES 数组? PHP 告诉我 $i 值 0、1、2、3.... 无效。

(附:我正在使用我在 Retrieving file names out of a multi-file upload control with javascript 看到的启用多个文件上传的示例中的 input type="file"multiple="multiple"name="uploaded"id="id_upload"代码)

最佳答案

你的名字应该是数组:

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

关于php - 无法从 php 中的 $_FILES 获取多个上传的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8469838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com