Download multiple images with one input field

I created a photo website with an admin page that uploads photos into different categories in the mysql table. This works a lot, but I can only upload one file at a time, and I would like to be able to select multiple images.

Here is the form

<form action="index.php" enctype="multipart/form-data" name="myForm" id="myForm" method="post">

    <select name="category" id="category">
        <option value="Nature">Nature</option>
        <option value="People">People</option>
        <option value="Abstract">Abstract</option>
    </select>

    <input type="file" name="fileField" id="fileField" />

    <input type="submit" name="submit" id="submit" value="Add Images" />

</form>

And here is php for parsing the form

if (isset($_POST['submit'])) {

$category = mysql_real_escape_string($_POST['category']);
// Add this product into the database now
$sql = mysql_query("INSERT INTO images (category, date_added) 
    VALUES('$category',now())") or die (mysql_error());


 $pid = mysql_insert_id();
// Place image in the folder 
$newname = "$pid.jpg";
move_uploaded_file( $_FILES['fileField']['tmp_name'], "../photos/$newname");
header("location: thumbnail_generator.php"); 
exit();
}

I looked at the input method of the html5 file, and as far as I can tell, I can change the input as folllows:

<input type="file" name="fileField[]" id="fileField" multiple="multiple"/>

This allows me to select multiple files on the site, but I cannot figure out how to implement this in my php. Any help would be greatly appreciated.

+5
source share
6 answers
+4
<form method="post" action="" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="500000">
Add Photos <input multiple="true" onchange="this.form.submit()" type="file" name="photos[]"/>

<input type="hidden" name="sendfiles" value="Send Files" />
</form>

    <?php
define ("MAX_SIZE","5000"); 
function getExtension($str)
{
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
}

$errors=0;

if(isset($_POST['sendfiles'])) 
{
  $uploaddir = "files/"; //a directory inside
    foreach ($_FILES['photos']['name'] as $name => $value)
    {
        $filename = stripslashes($_FILES['photos']['name'][$name]);
     //get the extension of the file in a lower case format
          $extension = getExtension($filename);
         $extension = strtolower($extension);
        echo "\n This is the extension: ",$extension;
         if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) 
         {
        //print error message
        ?>
           <h4>Unknown extension!</h4>
          <?php
             $errors=1;
         }
        else
        {
            $size=filesize($_FILES['photos']['tmp_name'][$name]);
            if ($size > MAX_SIZE*1024)
            {
            ?>
               <h4>You have exceeded the size limit!</h4>
              <?php
                $errors=1;
            }
            $image_name=$filename.'.'.$extension;
            $newname="files/".$image_name;
            $copied = copy($_FILES['photos']['tmp_name'][$name], $newname);
            if (!$copied) 
            {
                ?>
                <h4>Copy unsuccessfull!</h4>
                <?php
                $errors=1;
            }

        }

    }
    //echo "Images uploaded successfully";
}
//mysql_close($dbhandle);
?>
+5

: & shy; Docs, PHP. :

<html>
  <body>
    <form action="" method="post" enctype="multipart/form-data">
      <input type="file" name="fileField[]" id="fileField" multiple="multiple">
      <input type="text" name="test" value="test">
      <input type="submit" name="submit">
    </form>
  </body>
</html>
<?php
var_dump($_FILES, $_POST);

. , $_FILES $_POST.

:

array
  'fileField' => 
    array
      'name' => 
        array
          0 => string 'hakre-iterator-fun-cut-top.png' (length=30)
          1 => string 'hakre-iterator-fun-cut-middle.png' (length=33)
      'type' => 
        array
          0 => string 'image/png' (length=9)
          1 => string 'image/png' (length=9)
      'tmp_name' => 
        array
          0 => string '/tmp/php1A2.tmp' (length=15)
          1 => string '/tmp/php1A3.tmp' (length=15)
      'error' => 
        array
          0 => int 0
          1 => int 0
      'size' => 
        array
          0 => int 234001
          1 => int 213058
array
  'test' => string 'test' (length=4)
  'submit' => string 'Submit' (length=6)

. & shy; Docs.

+2

.

http://www.uploadify.com/demos/

Uploadify - script. Uploadify .

0

→ fileField []. , . , , script - http://valums.com/ajax-upload/

: , , mime! , .

0

? , ,

$images = array();

Then in his form just add the add button to the list of images? From there, he just continues to add as many images as he wants, and then just iterates over the array and adds to the database?

0
source

All Articles