Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Nov 2013
    Posts
    12
    Thanks
    6
    Thanked 0 Times in 0 Posts

    PHP Multiple File Upload

    Hey All,

    So I've never had to deal with multiple file uploads before with PHP but it looks like I'm going to need to learn how.

    Basically I'm attempting to create a form which uploads multiple files and then saves info about those files in separate rows in a table in a database.

    Here is what I have so far:

    HTML Form

    Code:
    <table style="border: 1px;">
            <tr>
                 <td>Image:</td>
                 <td><input type="file" name="image1"></td>
    	     <td>Image Name:</td>
    	     <td><input type="text" name="name1"></td>
    	     <td>Image Description:</td>
    	     <td><input type="text" name="desc1"></td>
    	     </td>							
    	</tr>
            <tr>
            <tr>
                 <td>Image:</td>
                 <td><input type="file" name="image2"></td>
    	     <td>Image Name:</td>
    	     <td><input type="text" name="name2"></td>
    	     <td>Image Description:</td>
    	     <td><input type="text" name="desc2"></td>
    	     </td>							
    	</tr>
            <tr>
                 <td>Image:</td>
                 <td><input type="file" name="image3"></td>
    	     <td>Image Name:</td>
    	     <td><input type="text" name="name3"></td>
    	     <td>Image Description:</td>
    	     <td><input type="text" name="desc3"></td>
    	     </td>							
    	</tr>
    PHP to Process Form and Upload Files

    PHP Code:
    <?php
    $image1
    =$_POST['image1'];
    $image2=$_POST['image2'];
    $image3=$_POST['image3'];
    $name1=$_POST['name1'];
    $name2=$_POST['name2'];
    $name3=$_POST['name3'];
    $desc1=$_POST['desc1'];
    $desc2=$_POST['desc2'];
    $desc3=$_POST['desc3'];

    $connection=mysql_connect($host,$uname,$pass) or die("connection in not ready <br>");
    $result=mysql_select_db($database) or die("database cannot be selected <br>");

    mysql_query("INSERT INTO pics (Filename, Title, Description) VALUES ('$image1','$name1','$desc1'),('$image2','$name2','$desc2'),('$image3','$name3','$desc3')");

    UPLOAD STUFF GOES HERE
    ?>
    Any chance any of you can help me know what would go into the "UPLOAD STUFF GOES HERE" section? Right now I'm not too concerned about security, I'm just trying to get this work. I'll be the only person using this at first anyway. At any rate any help you could give would be really helpful. I've done single file upload stuff before but never multiple file upload like this. Again I'd realllllly appreciate any help any of you could give!

    THANKS!

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,108
    Thanks
    11
    Thanked 101 Times in 99 Posts
    change the input names to e.g. images[] rather than image1, image2 etc
    Code:
    im1 <input type="file" name="images[]">
    im2 <input type="file" name="images[]">
    etc
    then do as you have previously done but in a loop...

    PHP Code:
        foreach($_FILES['images']['name'] as $idx=>$name){
            if(
    is_uploaded_file($_FILES['images']['tmp_name'][$idx])){
                       
    #whatever you want to do with the file here
               #e.g. move_uploaded_file($_FILES['tmp_name'][$idx],IMG_PATH."/{$idx}.png";
            
    }
        } 
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •