Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
608 views
in Technique[技术] by (71.8m points)

php - Uploading multiple images with one input field

i have created a photography website with an admin page that uploads photos to different categories in a mysql table. This much works, but i can only upload one file at a time and i'd like to be able to select multiple images.

Here's 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's the 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 into the html5 file input method, 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 can't figure out how to implement this into my php. Any help would be much appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
<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 "
 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);
?>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...