SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 35 of 35

Thread: How To Upload Multiple Images

  1. #26
    SitePoint Addict lespaulsf's Avatar
    Join Date
    Dec 2006
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats pretty cool thanks.

    So in my case how I would it from inserting info into the db for all upload boxes when I only want to upload one image and not three of them. Is there a loop or something that can do this?

  2. #27
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,538
    Mentioned
    79 Post(s)
    Tagged
    3 Thread(s)
    Do you mean how could you stop it adding 3 records into the database if you have only selected 1 image?
    Mike Swiffin - Community Team Leader

    Only a woman can read between the lines of a one word answer.....
    I started out with nothing... and still got most of it left!

  3. #28
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,538
    Mentioned
    79 Post(s)
    Tagged
    3 Thread(s)
    I guess thats what you mean so....

    After this line:
    PHP Code:
        for($i=0$i<=($numberOfUploads-1); $i++){ 
    add another check to see if the error code is there, just as you did earlier in the script.
    PHP Code:
        for($i=0$i<=($numberOfUploads-1); $i++){

           if(
    $_FILES['postPIC']['error'][$i] != 4) {   
           
    // carry on the processing and database insert





           
    // dont forget the closing brace at the end of the processing! 
    Mike Swiffin - Community Team Leader

    Only a woman can read between the lines of a one word answer.....
    I started out with nothing... and still got most of it left!

  4. #29
    SitePoint Addict lespaulsf's Avatar
    Join Date
    Dec 2006
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes that is what I mean, sorry for the confusion.

    If I only select one image then I only want it to insert the info in the db for that image.

    That works perfectly. Your a great help thanks so much.

    How did you learn so much about PHP? How long has it taken you to get to be so good at it?

  5. #30
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,538
    Mentioned
    79 Post(s)
    Tagged
    3 Thread(s)
    Started off in about 2004 just before I joined Sitepoint - in fact that was the reason for joining!

    The thing with php is that it is quite easy to get to grips with the basic functions and what they do, then it;s a case of putting it all together with a little help here and there
    Mike Swiffin - Community Team Leader

    Only a woman can read between the lines of a one word answer.....
    I started out with nothing... and still got most of it left!

  6. #31
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is interesting post because I'm having the same problem with uploading multiple images.

    I'm not sure how to print out the images from database when using array - do I need to change headers or use the array somehow in the printing process?

    I'm using following code to print one image:
    header('Content-Length: '.strlen($pic));
    header("Content-type: image/{$type}");

    echo $pic;

    How should I change it when I have multiple images stored in array and I want to print them all?

  7. #32
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,538
    Mentioned
    79 Post(s)
    Tagged
    3 Thread(s)
    Why are they stored in an array ggnome? have you pulled them out of the database and then into an array or is this on the upload part?
    Mike Swiffin - Community Team Leader

    Only a woman can read between the lines of a one word answer.....
    I started out with nothing... and still got most of it left!

  8. #33
    SitePoint Member
    Join Date
    Jan 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm storing images as binarydata (BLOB) into table. I have only one row called 'data' to store the images. I'm confused if I should have row for every image I upload?

    The problem is I don't understand how the uploaded data is stored in the table and how to pull it out..

    I'm quite new with php and mysql so I'm sometimes confused with the basics =)

  9. #34
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,538
    Mentioned
    79 Post(s)
    Tagged
    3 Thread(s)
    Well there are 2 ways to handle file uploads, you have done one and the other is to simple upload the file to it's destination directory and store the filename in the database rather than the full image data.

    Makes life much easier!
    Mike Swiffin - Community Team Leader

    Only a woman can read between the lines of a one word answer.....
    I started out with nothing... and still got most of it left!

  10. #35
    SitePoint Addict jamus's Avatar
    Join Date
    Jul 2004
    Location
    Devon, UK
    Posts
    297
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ View Post
    PHP Code:

    for($i=0$i<=$numberOfUploads$i++){ 
    The incremented number $i basically works through the $_FILES['postPIC']['name'] one at a time.

    HTH
    I was working through your example trying to apply it to my upload script but it line above throws an error for me. Any chance you could help me out?

    I don't think Im applying the looping around my function correctly:

    PHP Code:
    <?


    $uploadDir 
    $_SERVER['DOCUMENT_ROOT'].'/slideshow/datafiles/';

    // if upload ...
    if(isset($_POST['upload']))
    {

    $tmp_pic_array = array();

    $tmp_pic_array $_FILES['userfile']['tmp_name'];

        
    // count the number of $_FILES sent 
        
    $numberOfUploads count($_FILES);     
        
        if(
    $numberOfUploads == 0) { 
            echo 
    '<p>You selected not to add a picture to this post.</p>'
            
               } else { 
             
        for(
    $i=0$i<=$numberOfUploads$i++){ 

        
    $fileName $_FILES['userfile']['name'][$i];
        
    $tmpName  $_FILES['userfile']['tmp_name'][$i];
        
    $fileSize $_FILES['userfile']['size'][$i];
        
    $fileType $_FILES['userfile']['type'][$i];
        
        }

     
    // the files will be saved in filePath 
        
    $filePath $uploadDir $fileName;
        
        
    // move the files to the specified directory
        // if the upload directory is not writable or
        // something else went wrong $result will be false
        
    $result    move_uploaded_file($tmpName$filePath);
        if (!
    $result) {
            echo 
    "Error uploading file";
            exit;
        }
        
        include 
    'library/config.php';
        include 
    'library/opendb.php';

        if(!
    get_magic_quotes_gpc())
        {
            
    $fileName  addslashes($fileName);
            
    $filePath  addslashes($filePath);
        }  

        
    $query "INSERT INTO upload (files_name, files_userid, files_size, files_type, files_path ) ".
                 
    "VALUES ('$fileName', '$userID', '$fileSize', '$fileType', '$filePath')";

        
    mysql_query($query) or die('Error, query failed : ' mysql_error());                    

        include 
    'library/closedb.php';
        
        echo 
    "<br>File uploaded<br>";
        }        
    ?>

Bookmarks

Posting Permissions

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