Solved

uploading multiple images using php and mysql database

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

744 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

10 Experts available now in Live!

Get 1:1 Help Now