Solved

uploading multiple images using php and mysql database

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP_POST() error message 9 45
WordPress Themes 10 35
hiding/removing php extension - best practice 4 27
using php variable inside javascript 5 16
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

864 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now