Solved

uploading multiple images using php and mysql database

Posted on 2007-11-18
3
5,755 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exporting image to excel from php document 10 28
Wordpress Pagination Function Not working ? 7 32
paypal ipn to mysql 3 39
Which userID to use for shopping cart application 13 19
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…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

810 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