Paula Fleck
Paula Fleck

Reputation: 893

Simple upload multiple images in MySQL

I'm trying to do a simple upload in my system, just with the multiple="true" input property. Actually, I have 15 file inputs, and they all rename the file name to: 'id.in.the.bd'_'number'.jpg (Ex.: 65_1.jpg). Like this:

HTML

    // inside a form
    <?php for ($x = 1; $x <= 15; $x++) { ?>
       <input name="foto_<?php echo $x; ?>" type="file" id="foto" multiple="true" />
    <?php } ?>

PHP

if ($_GET['action'] == "insert") {


//sql insert


// do a consult to find the $id
$sql = mysql_query("SELECT * FROM imoveis ORDER BY id DESC LIMIT 1", $link); 

$result = mysql_fetch_array($sql, MYSQL_ASSOC);
$id = $result['id'];

for ($x = 1; $x <= 15; $x++) {
   $file = $_FILES["foto_" . $x];
   $path = '../../images/galeria/' . $id . '_' . $x . '.jpg';
   move_uploaded_file($file["tmp_name"], $path);
}

How can I easily change my code to use the multiple="true"? If I just change the input to multiple="true", he just upload the first image selected.

Upvotes: 0

Views: 1289

Answers (1)

Change the input tags to

<input name="foto[]" id="foto_1" type="file" />

In PHP these files are accessible by $_FILES['foto']['name'] array, like this:

$_FILES['foto']['name'][0] for file 1
$_FILES['foto']['name'][1] for file 2

etc..

Upvotes: 1

Related Questions