I am creating an upload script with PHP and HTML that could have any number of file uploads. I loop through and echo out any number of file uploads and name each one "fileField1", "fileField2" and so on using a loop.
Now I want to retrieve the name of the file in that field. Can I use a variable in another loop like this?
$fileField = 'fileField'.$i;
$fileName = $_FILES[$fileField]['name']; // Name of file
This is contained within the loop, will this work?
I believe I am experiencing an error...
Variable $i is increased by one each time ($i++)
EDIT
Notice the comment that I never reach...
Here is the whole loop:
$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++;
}