Solved

uploading multiple images using php and mysql database

Posted on 2007-11-18
3
5,795 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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 i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

622 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