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
    Regular Coder
    Join Date
    Jun 2007
    Posts
    310
    Thanks
    86
    Thanked 3 Times in 3 Posts

    Uploading multiple images?

    I'm wondering if someone can help me convert my single image uploader to a multiple file uploader.

    Basically it takes the image, renames it and uploads it to a folder on my server, with the filename of the image held in my database:

    PHP Code:
    <?php 
    if (isset($_POST['Submit'])) 

          
    // set the image details and upload the file
          
    $picunique time();
          
    $picture "/home/******/images/$picunique.jpg";
          
    move_uploaded_file ($_FILES['ufile']['tmp_name'][0], $picture);
        
          
    // and insert into the database  
          
    $result mysql_query("Insert into image_test (picture) values('"basename($picture) ."')") or die(mysql_error());
          
    $picture_id=mysql_insert_id();
          
    $error "Sucess - everything uploaded";

    ?>
    <form action="" method="post" enctype="multipart/form-data" name="myform" id="myform">
    <h3>Submit your Pic</h3>
    <?php echo $error;?>
      <fieldset>
        <legend>Please Upload your picture below: </legend> 
      <p> 
        <label for="Main Picture">Upload Image:</label> 
        <input name="ufile[]" type="file" id="ufile[]" />
      </p>
      <input name="Submit" type="submit" value="Submit" />
      <input name="Reset" type="reset" value="Reset" />  
      </fieldset>
    </form>
    </body>
    </html>
    Now, i've started the creation of my multiple uploader:

    Here

    with ?num= being any number, and that creates how many upload fields I get on the page dynamically:

    PHP Code:
    <form action="" method="post" enctype="multipart/form-data" name="myform" id="myform">
    <?php
    $number 
    $_GET['num']; 
    for(
    $i 0$i <= $number$i++)
    {
    echo 
    "
    <p>Upload Image ("
    .$i."):<input name=\"ufile[]\" style=\"width:300px;\" type=\"file\" id=\"ufile[]\" /></p>\n";
    }
    ?>
    </form>
    So i'd like to it so it re-names each image like the single uploader I created and saves the filename row, by row in the database.

    Can anyone kindly advise as i'm not sure how this can be done?

    Thanks

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You just need to loop your uploaded file processing code using foreach().

    Here's a start (not a complete snippet):

    PHP Code:
          // set the image details and upload the file
        
    foreach ($_FILES['ufile']['name'] as $key => $val) {
          
    $picunique microtime();
          
    $picture "/home/******/images/$picunique.jpg";
          
    move_uploaded_file ($_FILES['ufile']['tmp_name'][$key], $picture);
        
          
    // and insert into the database  
          
    $result mysql_query("Insert into image_test (picture) values('"basename($picture) ."')") or die(mysql_error());
          
    $picture_id=mysql_insert_id();
        }
        
    //Need to actually check the errors before declaring everything a success
          
    $error "Sucess - everything uploaded"
    You really should add some error checking in there as well so you can report back to the user when and if something goes wrong with the upload process. (Errors are stored in $_FILES['ufile']['error'][$key])

    (Note I used microtime() rather than time() as you'll probably process all files in less than a second)

  • Users who have thanked Fumigator for this post:

    stfc_boy (03-26-2009)


  •  

    Posting Permissions

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