Solved

uploading multiple images using php and mysql database

Posted on 2007-11-18
3
5,763 Views
Last Modified: 2013-12-13
Basically I have a three step form where the user first adds some text data.
This data is passed through to the second part of the form (using hidden fields). The second part of the form is where I'm having problems. I am trying to make the user be able to upload multiple images (max. around 10 images).

I'v been searching the internet forever and came across some scripts but I can't really seem to implement any of them very well.

So, I want the images to be uploaded to the server and stored in a folder ("upload"). The path of where the image is located should be stored in a table. In fact the table will consist of (unique/auto increment) ID field, unique file name and path field (path to where the img is located).

As far as the unique file name goes, it should also auto increment and thus be unique at all times (001,002, 003 etc.). I guess I could use the unique file name as a primary key instead of an ID field right?

I would also like the user to be able to upload the images and php automatically have a thumbnail of the particular image stored inside the "upload" folder. The goal of this is that there will be the original image and a thumbnail version (40x40px) stored in a folder for later use.

Anyway, can anyone get me started/point me in the right direction?

Here's the form I have so far.
<form action="insert_upload.php" method="POST" enctype="multipart/form-data">
 
<!-- Hidden input values carried through from previous form -->
<input type="hidden" name="title" value="<? echo $_POST['title'] ?>" >
<input type="hidden" name="description" value="<? echo $_POST['description'] ?>" >
 
<!-- Add current fields ie. IMAGES -->
<p><input type="file" name="userfile" id="img1"></p>
<p><input type="file" name="userfile" id="img2"></p>
<p><input type="file" name="userfile" id="img2"></p>
 
<p class="submit"><input type="submit" value="Proceed"></p>
 
</form>

Open in new window

0
Comment
Question by:thephiller
3 Comments
 
LVL 24

Accepted Solution

by:
mankowitz earned 225 total points
ID: 20307535
From php docs:

You need to put the square brackets after the filename in order for it to automatically put them into an array, like this:

<p><input name="userfile[]" type="file" /></p>
<p><input name="userfile[]" type="file" /></p>

Next, to access those files,
foreach ($_FILES["userfile"]["error"] as $key => $error) {
   if ($error == UPLOAD_ERR_OK) {
       echo"$error_codes[$error]";
       move_uploaded_file(
         $_FILES["userfile"]["tmp_name"][$key],
         "./upload/" . $_FILES["userfile"]["tmp_name"][$key]
       ) or die("Problems with upload");
     mysqli_query('INSERT INTO xxxx (path,filename) VALUES ("upload","{$_FILES["userfile"]["tmp_name"][$key]}")');

    //make thumbnail... you should modify if source image is png or gif
$src_img=imagecreatefromjpeg(  "./upload/" . $_FILES["userfile"]["tmp_name"][$key] );
$dst_img=ImageCreateTrueColor(40,40);
imagecopyresampled($dst_img,$src_img,0,0,0,0,40,40,imagesx($src_img), imagesy($src_img));
imagejpeg($dst_img, "./upload/thumb-" . $_FILES["userfile"]["tmp_name"][$key]

   }
}


See http://icant.co.uk/articles/phpthumbnails/ for making thumbnails
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question