Php 上载$\u文件时出现While循环错误

Php 上载$\u文件时出现While循环错误,php,arrays,file,while-loop,Php,Arrays,File,While Loop,我期待着上传多个文件到一个文件夹,一旦所有的条件都得到成功满足。我允许用户选择要上载的文件数量,但我从脚本收到此错误: 注意:第54行的C:\xampp\htdocs\freecese\news\u parse.php中未定义的偏移量:1 注意: // Set the array object to 0 when entering the loop. $i = 0; while ($_FILES['upload1']['tmp_name'][$i]) { $imgName1 = preg_rep

我期待着上传多个文件到一个文件夹,一旦所有的条件都得到成功满足。我允许用户选择要上载的文件数量,但我从脚本收到此错误:

注意:第54行的C:\xampp\htdocs\freecese\news\u parse.php中未定义的偏移量:1

注意:

// Set the array object to 0 when entering the loop.
$i = 0;
while ($_FILES['upload1']['tmp_name'][$i]) {
$imgName1 = preg_replace("#[^a-z0-9.]#i", "", $_FILES['upload1']['name'][$i]);

// Applies a unique number before the file name to prevent files from overwriting.
$imgName1 = mt_rand(100000, 999999).$imgName1;

// Moves the image into the images/ folder
move_uploaded_file($imgTmp1[$i], "images/$imgName1");

// Sets the next array object in the loop to 1 etc etc
   $i ++;
}
“Undefined offset:”后面的数字在本例中设置为“1”。当我选择在页面中插入更多文件字段时,这个数字就变成了PHP读取的当前文件字段的数量

例如:我有三个文件字段,错误会变成:

// Set the array object to 0 when entering the loop.
$i = 0;
while ($_FILES['upload1']['tmp_name'][$i]) {
$imgName1 = preg_replace("#[^a-z0-9.]#i", "", $_FILES['upload1']['name'][$i]);

// Applies a unique number before the file name to prevent files from overwriting.
$imgName1 = mt_rand(100000, 999999).$imgName1;

// Moves the image into the images/ folder
move_uploaded_file($imgTmp1[$i], "images/$imgName1");

// Sets the next array object in the loop to 1 etc etc
   $i ++;
}
注意:第54行C:\xampp\htdocs\freecese\news\u parse.php中未定义的偏移量:3

如果我选择了三个要上传的文件,它们都被正确地插入到了文件夹中,所以我不知道为什么当它正常工作时会出现错误

如果有人能帮我纠正这个错误,那将不胜感激

事先谢谢你,里奇

这是我的代码:

// Set the array object to 0 when entering the loop.
$i = 0;
while ($_FILES['upload1']['tmp_name'][$i]) {
$imgName1 = preg_replace("#[^a-z0-9.]#i", "", $_FILES['upload1']['name'][$i]);

// Applies a unique number before the file name to prevent files from overwriting.
$imgName1 = mt_rand(100000, 999999).$imgName1;

// Moves the image into the images/ folder
move_uploaded_file($imgTmp1[$i], "images/$imgName1");

// Sets the next array object in the loop to 1 etc etc
   $i ++;
}

如果有5个文件,则
$\u文件['upload1']['tmp\u name'][5]
将不存在,因此
while
条件崩溃(不会返回false!)。。。 您必须检查计数($\u文件['upload1']['tmp\u名称]])


你是一个美丽的人。干杯,伙计我会在9分钟内接受你的回答,谢谢你的快速响应。讽刺的是,他是一个随机的家伙@Filthy\u Rich你认为在这个while循环中使用SQL查询来插入数据库的文件路径安全吗?@Filthy\u Rich你是什么意思?