Multiple Image Upload to Server & Name to Databse

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ribbo
    New Member
    • Jan 2008
    • 9

    Multiple Image Upload to Server & Name to Databse

    What I need in a nutshell is a form that will allow me to upload multiple pictures (say 5) to my server while at the same time saving the names to the MySQL database. I would prefer it if the image name was automatically changed with MD5 or similar before upload and entry, but is not so important as I can add at a later date. I also would like (on the same form) the ability to add other information. i.e. make, model, etc.

    Example:

    Image 1: [Browse]
    Image 2:
    Image………

    Make:
    Model…………

    If there is anything anyone can do to help I would really
    appreciate it.
  • stepterr
    New Member
    • Nov 2007
    • 157

    #2
    Originally posted by ribbo
    What I need in a nutshell is a form that will allow me to upload multiple pictures (say 5) to my server while at the same time saving the names to the MySQL database. I would prefer it if the image name was automatically changed with MD5 or similar before upload and entry, but is not so important as I can add at a later date. I also would like (on the same form) the ability to add other information. i.e. make, model, etc.

    Example:

    Image 1: [Browse]
    Image 2:
    Image………

    Make:
    Model…………

    If there is anything anyone can do to help I would really
    appreciate it.
    Ribbo,
    Check out this site for a free script to get you started, Multiple File Upload Form . I used this as an example for myself and added more fields to the form for information that would be added to my database and then within the for loop and after the file was sucessfully uploaded I did my database INSERT statement. Hope that helps!

    Comment

    • ribbo
      New Member
      • Jan 2008
      • 9

      #3
      Thanks,

      I will check it out :)

      Comment

      • ribbo
        New Member
        • Jan 2008
        • 9

        #4
        unfortunately that didn't help to much (maybe its just me)
        I know some basic php and some advanced php. Although i am very patchy as i am self taught.

        I would be truly grateful to anyone who can resolve my problem.

        Here is the form.html page
        Code:
        <form enctype="multipart/form-data" action="add.php" method="post">
        Upload Image: <input name="userfile" type="file">
        <input type="submit" value="Upload File"></form>
        Here is the add.php page
        Code:
        <?php
        
        $username = "";
        $password = "";
        $database = "";
        $hostname = "";
        
        mysql_connect($hostname,$username,$password);
        mysql_select_db($database) or die( "Unable to select database");
        
        if ($userfile_size >250000){$msg=$msg."Your uploaded file size is more than 250KB. Please reduce the file size.<BR>";
        $file_upload="false";}
        
        if (!($userfile_type =="image/pjpeg" OR $userfile_type=="image/gif"))
        {echo "Your uploaded file must be of JPG or GIF. Other file types are not allowed<BR>";
        $file_upload="false";
        exit;}
        
        function findexts ($filename) 
        {
        $filename = strtolower($filename) ; 
        $exts = split("[/\\.]", $filename) ; 
        $n = count($exts)-1; 
        $exts = $exts[$n]; 
        return $exts; 
        }
        
        $ext = findexts ($_FILES['userfile']['name']) ;
        $ran = rand () ;
        $ran2 = $ran.".";
        $add = "upload/";
        $add = $add . $ran2.$ext;
        
        if(move_uploaded_file($_FILES['userfile']['tmp_name'], $add)){
        
        $query = "INSERT INTO car (image)". "VALUES ('$ran2$ext')";
        mysql_query($query) or die('Database Query Error!');
        
        echo "Successfully uploaded the image";
        chmod("$add",0777);
        }
        else{echo "Failed to upload file Contact Site admin to fix the problem";
        exit;}
        
        ?>
        As you can see i created this script so i can upload an image to the server, and the name to the database.

        what i now need is the ability to upload multiple images at the same time, with the same form.

        Thanks

        Comment

        • stepterr
          New Member
          • Nov 2007
          • 157

          #5
          hi ribbo,

          Have you used a for loop before? That's basically the only thing you are missing in your code to make it possible for you to do multiple files and its what that scripted I referenced earlier uses. The input field for type='file' will not let you select more than one file per input field so you'll need to either hard code in an input box for each file you want the user to be able to upload, or you can do it dynamically with a for loop.

          Then when you get to your add.php page you'll just need another for loop to go through your steps for each file.

          Does that clear things up a little bit?

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by stepterr
            hi ribbo,

            Have you used a for loop before? That's basically the only thing you are missing in your code to make it possible for you to do multiple files and its what that scripted I referenced earlier uses. The input field for type='file' will not let you select more than one file per input field so you'll need to either hard code in an input box for each file you want the user to be able to upload, or you can do it dynamically with a for loop.

            Then when you get to your add.php page you'll just need another for loop to go through your steps for each file.

            Does that clear things up a little bit?
            No! Write to whole code for him.

            NOW!

            Comment

            • ribbo
              New Member
              • Jan 2008
              • 9

              #7
              I dont mind hard coding in an input field for each one, i would actually prefer to.
              I just dont know how to make it work.

              Sorry to anyone who is offended by me asking for help!
              i am still new to php, but if it makes any difference i am reading and learning all i can.

              i am sure in the near future i will also be helping others with there problems.

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by ribbo
                I dont mind hard coding in an input field for each one, i would actually prefer to.
                I just dont know how to make it work.

                Sorry to anyone who is offended by me asking for help!
                i am still new to php, but if it makes any difference i am reading and learning all i can.

                i am sure in the near future i will also be helping others with there problems.
                I was only joking, friend!

                Say you had 3 input fields, yes? You name them 'name="file[]" ' that would then create an array out of the files.
                On the upload page, you'd run through them with a foreach loop executing the code.

                I've never actually done a multiple file upload... something i shall do right now!

                Comment

                • stepterr
                  New Member
                  • Nov 2007
                  • 157

                  #9
                  Originally posted by markusn00b
                  I was only joking, friend!

                  Say you had 3 input fields, yes? You name them 'name="file[]" ' that would then create an array out of the files.
                  On the upload page, you'd run through them with a foreach loop executing the code.

                  I've never actually done a multiple file upload... something i shall do right now!
                  Markusn00b is correct. You just need to make a few slight changes to accomplish this. I typically use a for loop when I create my input fields in situations like this so what I would do is something like this for the 5 fields you want :
                  [PHP]for($i=0; $i < 6; $i++)
                  {

                  echo "Upload field ".$i." &nbsp;<input name=\"userfile[]\" type=\"file\" id=\"userfile[]\" /><br />\n";
                  }[/PHP]

                  Now if you want to do this in HTML and not php and hard code each one then you may be able to do this for all 5 of your fields, but don't hold me to this because I haven't tried it yet.

                  [HTML]Upload Image: <input name="userfile[0]" type="file">
                  Upload Image: <input name="userfile[1]" type="file">
                  Upload Image: <input name="userfile[2]" type="file">[/HTML]


                  Now for the add.php page. I would take your function findexts and move it right below your database parameters since it can be called outside the for loop. Then take the rest of the code and place it in the for loop that will look like this:

                  [PHP]for($i=0; $i < 6; $i++)
                  {
                  //all of your upload code
                  }[/PHP]

                  Now how do you actually make sure that all of the files reference the correct name you ask? Well, for that you'll need to make use of the $i. So everywhere you are using the $_FILES['userfile']['name'] you'll need to add it like this:

                  [PHP]$_FILES['userfile']['name'][$i][/PHP]


                  Ok, after all of that you should be good to go.

                  Comment

                  • stepterr
                    New Member
                    • Nov 2007
                    • 157

                    #10
                    Originally posted by markusn00b
                    No! Write to whole code for him.

                    NOW!

                    See, you should've gone to bed when you said you were going to. Now you are just getting testy! ;-)

                    Comment

                    • Markus
                      Recognized Expert Expert
                      • Jun 2007
                      • 6092

                      #11
                      Originally posted by stepterr
                      See, you should've gone to bed when you said you were going to. Now you are just getting testy! ;-)
                      I'm going.

                      But i'm not happy. I hope you know that :(

                      Comment

                      • stepterr
                        New Member
                        • Nov 2007
                        • 157

                        #12
                        Originally posted by markusn00b
                        I'm going.

                        But i'm not happy. I hope you know that :(
                        ha! Before you go check your PMs!

                        Comment

                        • ribbo
                          New Member
                          • Jan 2008
                          • 9

                          #13
                          Thanks guys, i will use everything you have told me and try and make it work.
                          Appreciate it! :)

                          Comment

                          • jeff3188
                            New Member
                            • Feb 2009
                            • 2

                            #14
                            How to upload pictures and save it!!

                            Hello there....
                            I'm new here...
                            and i just want it to see your codes in saving the pictures in your database.
                            Last edited by jeff3188; Feb 7 '09, 10:42 AM. Reason: wrong grammar

                            Comment

                            • stepterr
                              New Member
                              • Nov 2007
                              • 157

                              #15
                              jeff3188,
                              Google searches are your friend. Its probably best if you try it and then let us know what trouble you are having and we can offer some assistance that way.

                              Comment

                              Working...