SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 35

Thread: How To Upload Multiple Images

Hybrid View

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

    How To Upload Multiple Images

    Hello

    I have been working through Kevin Yanks Book "Database Driven Website"

    I have completed the script that allows for a single file upload.

    What I do then is insert the image name into a table so I can later pull it out by linking it to the postID of another table.

    Anyway here is the script. I need to know how to make it accept and process multiple images. I know it has to do with a foreach function and a array but cant figure it out. I also know that in the html form I need to make them in array format under the name attribute of the input file tage.

    PHP Code:
    //check for picture upload
       
    if (isset($_FILES['postPIC'])){
           
           
    //get filename from client machine
           //strip file extension from filename  
           
    $original_file_name substr($_FILES['postPIC']['name'],0,-4);
        
        
    //if no picture was uploaded kill process
        
    if(!is_uploaded_file($_FILES['postPIC']['tmp_name'])){
            echo 
    '<p>You selected not to add a picture to this post.</p>';
        } else {
        
        
    //retrieve postID from tbl_blogpost
        
    $blogpostID mysql_insert_id();
        
        if (
    eregi('^image/p?jpeg(;.*)?$',
              
    $_FILES['upload']['type'])){
                
    $extension '.jpg';
        } else {
                
    $extension '.gif';
            }
                
        
    //The complete path/filename create name of file
        
    $filelocation 'C:/wamp/www/myblog/post_pics/';
        
    $filename $original_file_name $postDate $extension;
        
    $completefile $filelocation "" $filename;
        
        
    //copy file (if it'deemed safe)
        
    if (is_uploaded_file($_FILES['postPIC']['tmp_name'])and 
            
    copy($_FILES['postPIC']['tmp_name'],$completefile)){
              echo 
    "<p>Image stored successfully at <br />$completefile</p>";
            } else {
                echo 
    "<p>Could not save image!</p>";
            }
        
        
        
    //insert picture info into tbl_postimages
        
    $sql2 "INSERT INTO tbl_postimages SET 
                 postID='
    $blogpostID',
                 image_name='
    $filename' ";
        if (@
    mysql_query($sql2)){
            echo 
    '<p><i>Picture info inserted in DB</i></p>'.
                 
    '<a href="viewposts.php">Return </a>';
        } else {
            echo 
    '<p>Error adding picture info in db: ' .
            
    mysql_error(). '</p>';
        }
        }    
       } 
    Here is the form with the multiple input file tags. It is also used for posting and pulls out authors, but thats besides the point. I just need to learn how to process multiple imags.

    PHP Code:
    <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" 
        enctype="multipart/form-data">
            <h3>Create Blog Entry</h3>
            <p>Post Title:</p>
            <input type="text" name="postTitle" size="25"/><br />
            <p>Author:</p>
            <select name="postauthorID" size="1">
            <option selected value="">Select One</option>
            <option value="">-------</option>
    <?php

       
    //Display authors drop down box
       
    while ($authorrow mysql_fetch_array($authors)){
        
    $aid $authorrow['authorID'];
        
    $aname htmlspecialchars($authorrow['authorName']);
        echo 
    "<option value='$aid'>$aname</option>\n";
    }
    ?>
       </select>
               <p>Upload Pictures:</p>
               <input type="file" size="35" name="postPIC[]" /><br />
               <input type="file" size="35" name="postPIC[]" />
               <input type="file" size="35" name="postPIC[]" />
               
            <p>Blog Entry Text:</p>
            <div id="postoptions">Formatting Options | You must open and close each tag <br />
            Bold = [b] [eb] | example [b]<strong>This is bold text </strong>[eb]<br />
            Italics = [i] [ei] | example [i]<em>This is italic text </em>[ei]<br \> 
            Hyperlink URL = [l] [el] | example [l]http://www.somesite.com/[el]  
            
            </div>
            <textarea name="postText"></textarea><br /><br />
            <input type="submit" value="Post"/>
            <input type="reset" value="Reset" />
        </form>

  2. #2
    SitePoint Member RH-Yan's Avatar
    Join Date
    Jan 2008
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Loop through the $FILES array and then use the INSERT statement for each iteration...you do not need to use foreach loop, use for loop if that is easier for you

  3. #3
    SitePoint Addict lespaulsf's Avatar
    Join Date
    Dec 2006
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RH-Yan View Post
    Loop through the $FILES array and then use the INSERT statement for each iteration...you do not need to use foreach loop, use for loop if that is easier for you
    COuld you give me a example that is related to my code? What would be the difference using the for loop instead of the foreach?

  4. #4
    SitePoint Addict lespaulsf's Avatar
    Join Date
    Dec 2006
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can anyone help me please on this?

  5. #5
    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)
    When the array of uploaded files is sent through you need to iterate through it as RH-Yan mentioned.
    To do this you need to count the number of file inputs that have been sent through and use a for loop. Inside that for loop you run the script that uploads it and inserts the information into the database.

    PHP Code:

    if (isset($_FILES['postPIC'])){

    // 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++){

    ///////////////////////////////////////////////
    // Run the upload script and the add to the database part
    // You will need to go through the script and change 
    // $_FILES['postPIC']['name'] to $_FILES['postPIC']['name'][$i]
    // The $i is the incremented number
    ///////////////////////////////////////////////




    The incremented number $i basically works through the $_FILES['postPIC']['name'] one at a time.

    HTH
    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. #6
    SitePoint Addict lespaulsf's Avatar
    Join Date
    Dec 2006
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok great that makes some since. Do I need to also change the

    $_FILES['postPIC']['tmp_name'] to $_FILES['postPIC']['tmp_name'][$i]

    Also is the form portion correct does it need the [] for the array. The reason I ask is because the isset function looks for the $_FILES['postPIC'] instead of the postPIC[]

    <input type="file" size ="35" name="postPIC[]" />
    <input type="file" size ="35" name="postPIC[]" />
    <input type="file" size ="35" name="postPIC[]" />

  7. #7
    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>";
        }        
    ?>

  8. #8
    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)
    Yes, the form is right and the isset should still work.....
    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!

  9. #9
    SitePoint Addict lespaulsf's Avatar
    Join Date
    Dec 2006
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow fast response.

    Do i need to change $_FILES['postPIC']['tmp_name'] to $_FILES['postPIC']['tmp_name'][$i].

  10. #10
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lespaulsf View Post
    Wow fast response.

    Do i need to change $_FILES['postPIC']['tmp_name'] to $_FILES['postPIC']['tmp_name'][$i].
    I'd declare an array before the loop..

    PHP Code:
    $tmp_pic_array = array();

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


    for(
    $i=0$i<=$numberOfUploads$i++){
    $tmp_pic $tmp_pic_array[$i];
    //Then do your stuff as $tmp_pic will hold the value of the picture for each iteration of $i

    just a personal preference for readability. cheers!
    No, I REALLY dislike having to use Joomla.

  11. #11
    SitePoint Addict lespaulsf's Avatar
    Join Date
    Dec 2006
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok thanks so I'm getting there but not quite.

    So i have three input file upload boxes. The problem is that it will only upload the two images.

  12. #12
    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)
    Yes, sorry should have mentioned that. Because you are looping through the array, anywhere it has $_FILES['postPic']..... add the [$i] to the end
    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!

  13. #13
    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)
    ^ quite a clear way of doing it. Makes editing easy too
    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!

  14. #14
    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)
    Post your code as it stands now
    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!

  15. #15
    SitePoint Addict lespaulsf's Avatar
    Join Date
    Dec 2006
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok here you go

    PHP Code:
    //check for picture upload
        
    if (isset($_FILES['postPIC'])){
           
           
    // 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++){
           
           
    //get filename from client machine
           //strip file extension from filename  
           
    $original_file_name substr($_FILES['postPIC']['name'][$i],0,-4);
        
        
    //if no picture was uploaded kill process
        
    if(!is_uploaded_file($_FILES['postPIC']['tmp_name'][$i])){
            echo 
    '<p>You selected not to add a picture to this post.</p>';
        } else {
        
        if (
    eregi('^image/p?jpeg(;.*)?$',
              
    $_FILES['upload']['type'])){
                
    $extension '.jpg';
        } else {
                
    $extension '.gif';
            }
                
        
    //The complete path/filename create name of file
        
    $filelocation 'C:/wamp/www/myblog/post_pics/';
        
    $filename $original_file_name $postDate $extension;
        
    $completefile $filelocation "" $filename;
        
        
    //copy file (if it'deemed safe)
        
    if (is_uploaded_file($_FILES['postPIC']['tmp_name'][$i])and 
            
    copy($_FILES['postPIC']['tmp_name'][$i],$completefile)){
              echo 
    "<p>File stored successfully as <br />$completefile</p>";
            } else {
                echo 
    "<p>Could not save file as $completefile!</p>";
            }
        
        
    //insert picture info into tbl_postimages
        
    $sql2 "INSERT INTO tbl_postimages SET 
                 postID='
    $blogpostID',
                 image_name='
    $filename' ";
        if (@
    mysql_query($sql2)){
            echo 
    '<p><i>Picture info inserted in DB</i></p>'.
                 
    '<a href="viewposts.php">Return </a>';
         } else {
            echo 
    '<p>Error adding picture info in db: ' .
            
    mysql_error(). '</p>';
          }
         }    
        }
       }
     } 

  16. #16
    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)
    try
    PHP Code:
    //check for picture upload
        
    if (isset($_FILES['postPIC'])){
           
    // count the number of $_FILES sent
        
    $numberOfUploads count($_FILES['postPIC']['name']);
        
        
        if(
    $numberOfUploads == 0) {
             echo 
    '<p>You selected not to add a picture to this post.</p>';
        } else {
            
        for(
    $i=0$i<=($numberOfUploads+1); $i++){
           
           
    //get filename from client machine
           //strip file extension from filename  
           
    $original_file_name substr($_FILES['postPIC']['name'][$i],0,-4);
        
        
    //if no picture was uploaded kill process

        
    if (eregi('^image/p?jpeg(;.*)?$',
              
    $_FILES['postPIC']['type'][$i])){
                
    $extension '.jpg';
        } else {
                
    $extension '.gif';
            }
                
        
    //The complete path/filename create name of file
        
    $filelocation 'C:/wamp/www/myblog/post_pics/';
        
    $filename $original_file_name $postDate $extension;
        
    $completefile $filelocation "" $filename;
        
        
    //copy file (if it'deemed safe)
        
    if (is_uploaded_file($_FILES['postPIC']['tmp_name'][$i])and 
            
    copy($_FILES['postPIC']['tmp_name'][$i],$completefile)){
              echo 
    "<p>File stored successfully as <br />$completefile</p>";
            } else {
                echo 
    "<p>Could not save file as $completefile!</p>";
            }
        
        
    //insert picture info into tbl_postimages
        
    $sql2 "INSERT INTO tbl_postimages SET 
                 postID='
    $blogpostID',
                 image_name='
    $filename' ";
        if (@
    mysql_query($sql2)){
            echo 
    '<p><i>Picture info inserted in DB</i></p>'.
                 
    '<a href="viewposts.php">Return </a>';
         } else {
            echo 
    '<p>Error adding picture info in db: ' .
            
    mysql_error(). '</p>';
          }


        }
       }
     } 
    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!

  17. #17
    SitePoint Addict lespaulsf's Avatar
    Join Date
    Dec 2006
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok great its working now for the most part. It will upload the three images but at the end it still gives me the output of

    "You selected not to add a picture to this post"

    I know this is do to the !is_uploaded_file checking to see if its a file or not. My question is why would it loop through it again if it already has all of the files uploaded in the directory and in the db?

    Also is there another way to check and make sure a file has been uploaded?

    Heres my current code. Thanks very much for your help

    PHP Code:
    //check for picture upload
        
    if (isset($_FILES['postPIC'])){
           
           
    // count the number of $_FILES sent
        
    $numberOfUploads count($_FILES['postPIC']['name']);
        
        if(
    $numberOfUploads == 0) {
             echo 
    '<p>You selected not to add a picture to this post.</p>';
        } else {
            
        for(
    $i=0$i<=$numberOfUploads$i++){
           
           
    //get filename from client machine
           //strip file extension from filename  
           
    $original_file_name substr($_FILES['postPIC']['name'][$i],0,-4);
        
        
    //if no picture was uploaded kill process
        
    if(!is_uploaded_file($_FILES['postPIC']['tmp_name'][$i])){
            echo 
    '<p>You selected not to add a picture to this post.</p>';
        } else {
        
        if (
    eregi('^image/p?jpeg(;.*)?$',
              
    $_FILES['upload']['type'])){
                
    $extension '.jpg';
        } else {
                
    $extension '.gif';
            }
                
        
    //The complete path/filename create name of file
        
    $filelocation 'C:/wamp/www/myblog/post_pics/';
        
    $filename $original_file_name $postDate $extension;
        
    $completefile $filelocation "" $filename;
        
        
    //copy file (if it'deemed safe)
        
    if (is_uploaded_file($_FILES['postPIC']['tmp_name'][$i])and 
            
    copy($_FILES['postPIC']['tmp_name'][$i],$completefile)){
              echo 
    "<p>File stored successfully as <br />$completefile</p>";
            } else {
                echo 
    "<p>Could not save file as $completefile!</p>";
            }
        
        
    //insert picture info into tbl_postimages
        
    $sql2 "INSERT INTO tbl_postimages SET 
                 postID='
    $blogpostID',
                 image_name='
    $filename' ";
        if (@
    mysql_query($sql2)){
            echo 
    '<p><i>Picture info inserted in DB</i></p>'.
                 
    '<a href="viewposts.php">Return </a>';
         } else {
            echo 
    '<p>Error adding picture info in db: ' .
            
    mysql_error(). '</p>';
          }
         }    
        }
       }
     } 

  18. #18
    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)
    If you look at the code I did above, I took that part out as it's not needed as the check is done further up the script.

    PHP Code:
    if($numberOfUploads == 0) {

             echo 
    '<p>You selected not to add a picture to this post.</p>';

        } else { 
    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!

  19. #19
    SitePoint Addict lespaulsf's Avatar
    Join Date
    Dec 2006
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes I did try taking out the is_uploaded code but when I do it really messes up. It tries to upload a file that doesn't exist and then inputs the info into the database.

    I then decided to print out the $numberOfUploads. When I did I got 3, although when I printed out $i the following:

    0
    1
    2
    3

    I understand that arrays start at 0 but why would there be for instances if I only have 3 file upload boxes.

    Hence this is my problem now.

    Heres my code as of now. Note I added the suggestion of declaring the array first.


    PHP Code:
    //check for picture upload
        
    if (isset($_FILES['postPIC'])){
            
        
    //declare image array
        
    $tmp_pic_array = array(); 
        
        
    $tmp_pic_array $_FILES['postPIC']['tmp_name'];
            
           
    // count the number of $_FILES sent
        
    $numberOfUploads count($_FILES['postPIC']['name']);
        
        
    //echo " this is the number of uploads $numberOfUploads";
        
        
    if($numberOfUploads == 0) {
             echo 
    '<p>You selected not to add a picture to this post.</p>';
        } else {
            
        for(
    $i=0$i<=$numberOfUploads$i++){
            
    $tmp_pic $tmp_pic_array[$i];
           
           
    //get filename from client machine
           //strip file extension from filename  
           
    $original_file_name substr($_FILES['postPIC']['name'][$i],0,-4);
        
        if (
    eregi('^image/p?jpeg(;.*)?$',
              
    $_FILES['upload']['type'])){
                
    $extension '.jpg';
        } else {
                
    $extension '.gif';
            }
                
        
    //The complete path/filename create name of file
        
    $filelocation 'C:/wamp/www/myblog/post_pics/';
        
    $filename $original_file_name .'_ID' .$blogpostID $extension;
        
    $completefile $filelocation "" $filename;
        
        
    //copy file (if it'deemed safe)
        
    if (is_uploaded_file($tmp_pic)and 
            
    copy($tmp_pic,$completefile)){
              echo 
    "<p>File stored successfully as <br />$completefile</p>";
            } else {
                echo 
    "<p>Could not save file as $completefile!</p>";
            }
        
        
    //insert picture info into tbl_postimages
        
    $sql2 "INSERT INTO tbl_postimages SET 
                 postID='
    $blogpostID',
                 image_name='
    $filename' ";
        if (@
    mysql_query($sql2)){
            echo 
    '<p><i>Picture info inserted in DB</i></p>';
         } else {
            echo 
    '<p>Error adding picture info in db: ' .
            
    mysql_error(). '</p>';
          }
         }    
        
       }
     } 

  20. #20
    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)
    change
    PHP Code:
    for($i=0$i<=$numberOfUploads$i++){ 
    to
    PHP Code:
    for($i=0$i<=($numberOfUploads-1); $i++){ 
    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!

  21. #21
    SitePoint Addict lespaulsf's Avatar
    Join Date
    Dec 2006
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks very much. I guess I should have thought of that. O well thats what your the 2007 PHP Guru of the year.

    Thanks very much that fixed it.

  22. #22
    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)
    no problem
    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!

  23. #23
    SitePoint Addict lespaulsf's Avatar
    Join Date
    Dec 2006
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    FOund another problem. Now the check to make sure a image is uploaded is not working.

    PHP Code:
    if($numberOfUploads == 0) {
             echo 
    '<p>You selected not to add a picture to this post.</p>';
        } else { 
    Any if I dont add any images to upload it inserts in the db and it of course incorrect because no image was uploaded to a directory.

  24. #24
    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)
    change
    PHP Code:
    if($numberOfUploads == 0) { 
    to
    PHP Code:
    if($_FILES['postPIC']['error'][0] == 4) { 
    Should be more reliable
    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!

  25. #25
    SitePoint Addict lespaulsf's Avatar
    Join Date
    Dec 2006
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great thanks again. Can you explain what and how this piece of code works in relation to the upload process? The reason I ask is because if I only upload one image the db will try and insert values for the the other two file inputs I didn't upload.

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
  •