0

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.

Hero_Vega
  • 3
  • 1
  • 1
  • 3

2 Answers2

2

1- You should make an array with the list of image you want to stored

2- Use AutoIncrement in your column Id in the table(Important)

also you have the array you can do the query as follow:

capture the data from post

$IMG = isset($_POST['files']) ? $_POST['files'] : array();
if (!empty($IMG)) {
    $uploads_dir = 'images/costume/';
    foreach ($IMG["error"] as $key => $error) {
        if ($error == UPLOAD_ERR_OK) {
            $tmp_name = $IMG["tmp_name"][$key];
            $name = $IMG["name"][$key];
            move_uploaded_file($tmp_name, "$uploads_dir/$name");
            $name_array=mysql_real_escape_string($name);
            $value_insert[] = "('" . $name_array . "')";
        }
    }
    $values_insert = implode(',', $value_insert);
    $query = "INSERT INTO costumes (name) VALUES" . $values_insert;
    $result = mysql_query($query);
}else{
 echo 'empty array';
}

As side Note: Mysql_* extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

A useful link Why shouldn't I use mysql_* functions in PHP

Community
  • 1
  • 1
Emilio Gort
  • 3,475
  • 3
  • 29
  • 44
  • Isn't an array already created by ? And where would I incorporate this snippet? Because I still need the files to be uploaded to a directory on the server. – Hero_Vega Aug 01 '13 at 23:58
  • I tried it, but no files are uploaded to either the directory or the database. – Hero_Vega Aug 02 '13 at 00:23
1

You should not use "mysql_insert_id", but set the id row to auto increment in phpmyadmin.

Sevron
  • 493
  • 7
  • 18