www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Multiple image upload

  1. #1
    Join Date
    May 2006
    Location
    Perth, Scotland
    Posts
    226

    Multiple image upload

    Hey guys,

    I'm just wondering the best way to approach this.

    On my blog site, I have a place where I can write a blog post. Along with that I can add one image to a database.

    I want to add 5 images instead, so I can display these images in association with the given blog post at a later stage.

    So it works fine with 1 image and running the query once.

    I tried putting the query in a for loop and looping it 5 times, and looking in the $_FILES array for the right values.

    However i'm not sure if this was done correctly.

    PHP Code:
    for ($i 5$i <= 5$i += 1
    {                    
        
    $imgQuery sprintf("insert into journal_img
                         set
                         journal_id = '%s',
                         img_name = '%s',
                         img_size = '%s',
                         img_type = '%s',
                         img_path = '%s'
                         "

                         
    mysql_real_escape_string($fid),
                         
    mysql_real_escape_string($fileName),
                         
    mysql_real_escape_string($fileSize),
                         
    mysql_real_escape_string($fileType),
                         
    mysql_real_escape_string($filePath));

    The variable values have been extracted like this

    PHP Code:
    $fileName $_FILES['userfile']['name'];
    $tmpName $_FILES['userfile']['tmp_name'];
    $fileSize $_FILES['userfile']['size'];
    $fileType $_FILES['userfile']['type']; 
    Shooting from the hip here, I guess I need to access value 0 to 4 in the $_FILES array and increment the query values by 1 on the $_FILES array each time it loops to get the right values, however that might not be the right logic.

    Anyone who can help with this would be brilliant.

    Thanks in advance

    Alistair

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,616
    How are you naming the file-type input elements in your form?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    May 2006
    Location
    Perth, Scotland
    Posts
    226
    They are all the same, but i've been told not to do that.... I know with radio buttons etc you can add an array key in the html to a name

    name='this[1]' but not a good idea for this

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,616
    You can give them all the same name with a "[]" at the end, which will create a sub-array within PHP.
    Code:
    <input type="file" name="userfile[]" />
    Then you can loop through them as:
    PHP Code:
    foreach ($_FILES["userfile"]["error"] as $key => $error) {
       if (
    $error == UPLOAD_ERR_OK) {
          
    // do your move_uploaded_file(), etc. here with
          // $_FILES['userfile']['tmp_name'][$key], $_FILES['userfile']['name'][$key], etc....
       
    }
       else {
          
    // echo error or whatever here if you want for failed upload
       
    }

    Last edited by NogDog; 12-24-2008 at 12:31 AM.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    May 2006
    Location
    Perth, Scotland
    Posts
    226
    Hey NogDog,

    PHP Code:
    foreach ($_FILES["pictures"]["error"] as $key => $error

        if (
    $error == UPLOAD_ERR_OK
        {
            
    $uploadDir $_SERVER['DOCUMENT_ROOT']."/v1/jimg/";
            
            
    $fileName $_FILES['userfile']['name'][$key];
            
    $tmpName $_FILES['userfile']['tmp_name'][$key];
            
    $fileSize $_FILES['userfile']['size'][$key];
            
    $fileType $_FILES['userfile']['type'][$key];

            
    $filePath $uploadDir $fileName;

            
    $movefile move_uploaded_file($tmpName$filePath);

            if (!
    $movefile
            {
                echo 
    "<p>Could not upload images.</p>";
            }

            if(!
    get_magic_quotes_gpc())
            {
                
    $fileName addslashes($fileName);
                
    $filePath addslashes($filePath);
            }
            
            
    $imgQuery sprintf("insert into journal_img
                         set
                         img_name = '%s',
                         img_size = '%s',
                         img_type = '%s',
                         img_path = '%s'
                         "

                         
    mysql_real_escape_string($fileName),
                         
    mysql_real_escape_string($fileSize),
                         
    mysql_real_escape_string($fileType),
                         
    mysql_real_escape_string($filePath));                     
        }
        else 
        {
          echo 
    "Image error";
        } 

    Thanks for your help so far.

    This is now returning this error

    Code:
    Warning: Invalid argument supplied for foreach() in /home/alistair/public_html/v1/php/fns_journal.php on line 283
    Any ideas?

    EDIT* The query that was listed in the error is irrelevent and I have removed it.
    Last edited by a.g.r.c; 12-24-2008 at 12:43 AM.

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,616
    You copied-and-pasted my code before I edited it to fix the copy-and-paste error I had in it. $_FILES['picture'] should have been $_FILES['userfile'] (or whatever you want to name the field in the form).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    May 2006
    Location
    Perth, Scotland
    Posts
    226
    Ah nicely nicely nicely,

    Thanks Nog, lemme go try this out now.

    Was having a gander at your blog, your dog is cute. Hows the recovery process going now?

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,616
    Noggin seems to be recovering well from the surgery, thank you. Tomorrow (well, today, now) I'll probably find out if it was malignant or benign, which will then determine whether or not I have a merry Christmas.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    May 2006
    Location
    Perth, Scotland
    Posts
    226
    If i'm correct a benign lump is the better of the two.

    I'll say a little prayer <3 dogs, my Father trains guide dogs for the blind. Lovely animals.

  10. #10
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,616
    Yes: $malignant == $cancer, $benign != $cancer.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  11. #11
    Join Date
    May 2006
    Location
    Perth, Scotland
    Posts
    226
    Hey again,

    In regards to this, everything is going almost 100&#37; now, except from the query is a little bugged.

    Its coming back with a duplicate entry error, which makes me think its not taking the incremented values when entering the var values to the db at each itteration.

    PHP Code:
    $imgQuery sprintf("insert into journal_img
                         set
                         img_name = '%s',
                         img_size = '%s',
                         img_type = '%s',
                         img_path = '%s'
                         "

                         
    mysql_real_escape_string($fileName),
                         
    mysql_real_escape_string($fileSize),
                         
    mysql_real_escape_string($fileType),
                         
    mysql_real_escape_string($filePath)); 
    Is the query.

    Should it be something like?

    PHP Code:
    $imgQuery sprintf("insert into journal_img
                         set
                         img_name = '%s',
                         img_size = '%s',
                         img_type = '%s',
                         img_path = '%s'
                         "

                         
    mysql_real_escape_string($fileName[$key]),
                         
    mysql_real_escape_string($fileSize[$key]),
                         
    mysql_real_escape_string($fileType[$key]),
                         
    mysql_real_escape_string($filePath)[$key]); 
    Thanks again

  12. #12
    Join Date
    May 2006
    Location
    Perth, Scotland
    Posts
    226
    Also i'm planning on setting up a friends page on my site at somepoint....

    Link

    Would you mind if I added you?

  13. #13
    Join Date
    May 2006
    Location
    Perth, Scotland
    Posts
    226
    Hey,

    For some reason the table i set up had lost its primary key and auto_increment.

    I then altered the table and still had the same problem.

    Then I altered it again, which fixed it.

    I guess some things are just unexplainable.

    Thanks for all your help mate. That was awesome. Wooters!

  14. #14
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,616

    [off-topic]

    Just a quick follow-up on Noggin: the lab results are in and the verdict is "benign," so now I can actually enjoy the holidays.

    Thanks for you concern and support.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  15. #15
    Join Date
    May 2006
    Location
    Perth, Scotland
    Posts
    226
    Hey Nog,

    Merry Christ buddy, do you and Noggin.

    Alistair

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles