我正在使用 PHP 和 HTML 创建一个上传脚本,可以上传任意数量的文件。我循环并回显任意数量的文件上传,并使用循环将每个文件命名为“fileField1”、“fileField2”等。
现在我想检索该字段中的文件名。我可以像这样在另一个循环中使用变量吗?
$fileField = 'fileField'.$i;
$fileName = $_FILES[$fileField]['name']; // Name of file
这包含在循环中,这行得通吗?
我相信我遇到了一个错误...
变量 $i 每次加一 ($i++)
编辑
请注意我从未达到的评论...
这是整个循环:
$i = 1;
while ($i <= $numberOfFields) {
$fileField = 'fileField'.$i;
$titleId = 'title'.$i;
$genreId = 'genre'.$i;
$fileName = $_FILES[$fileField]['name']; // Name of file
if($fileName !=""){
$randNum = rand(1000000000,9999999999); //Generate random number
$newFileName = $fileName."-".$randNum; //Rename file
if (file_exists($baseUrl."images/".$newFileName)){
echo $newFileName." already exists. ";
}else{
if(isset($_POST[$titleId]) && !empty($_POST[$titleId])){
// Assign url, title, genre
$url = 'images/'.$newFileName;
$title = $_POST[$titleId];
$genre = $_POST[$genreId];
// Execute final query and upload
$sql = "INSERT INTO images (url,title,genre) VALUES (:url,:title,:genre)";
$q = $conn->prepare($sql);
$q->execute(array(':url'=>$url,
':title'=>$title,
':genre'=>$genre));
if(copy($_FILES['fileField']['tmp_name'],'../images/'.$newFileName)) {
$NOTICE = 'File successfully uploaded!';
// I AM NEVER REACHING THIS NOTICE WHICH IS ECHOED LATER ON IN THE CODE
}
}else{
$NOTICE = "Please fill out the title for picture ".$i;
break;
}
}
}else{
$NOTICE = "Please select a file to upload in slot ".$i;
break;
}
$i++;
}