I am building a site for a friend and have set up a form that will upload multiple images to the database. It will only be accessed by one person, and is not visible to the general public so for sake of easy coding I have ignored a lot of possible threats to the code in order to just get it working.
The HTML is simple it's just:
<form action="(url here)" method="post" enctype="multipart/form-data">
<input type="file" name="files[]" multiple>
<input id="formbutton" type= "submit" value="Upload!">
</form>
Here is the php that I'm worried about.
if(isset($_FILES['files'])){
foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name){
$target="images/costume/";
$target=$target.$_FILES['files']['name'][$key];
if(move_uploaded_file($tmp_name, $target)){
$id = mysql_insert_id($con);
mysql_query("INSERT INTO costumes (id,name) VALUES ('$id','$name')");
}
}
}
All of the image files upload to the directory on the server, but only ONE of the images actually stores into the MySQL table as a row with ID and file name.
I've been racking my brain and searching like mad for a solution, but there aren't any that have worked. I know it has something to do with the foreach loop but I'm at a loss of what to do to make every image I upload create a row in the database.
Thanks for the help guys.
Sorry, I was making changes and forgot to switch back to the original code for the last section:
if(isset($_FILES['files'])){
foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name){
$target="images/costume/";
$target=$target.$_FILES['files']['name'][$key];
if(move_uploaded_file($tmp_name, $target)){
$id = mysql_insert_id($con);
mysql_query("INSERT INTO costumes (id,name) VALUES ('$id','$target')");
}
}
}
That's how it should be.