Trouble uploading multiple images with this script.

Posted on 2007-11-21
Last Modified: 2013-12-12
I've got a script that currently successfully uploads a pic to a folder (folder name = upload) and then stores the path of the uploaded image in the database (don't want to overload the database of course :)

However I want to be able to upload more than one image, all at the same time. To be exact there will be 12 image upload fields and thus 12 images can be uploaded at the same time.

After doing some searching on the internet I came across some scripts that seem to advocate a for loop of some sort (is this recommended/the only way?) but I can't seem to get my head around implementing it into my current script.

Also, in my database i currently have an ID (auto increment) and PATH (path of the uploaded file) field.
I want all of the 12 images that are uploaded simultaneously to be entered into the same ID field (ie. they should all have the same ID).
Does this mean I need to add more fields for each of the 12 images?


Here's the script that uploads the image:


$uploadDir = "upload/";



$fileName = $_FILES['userfile']['name'];

$tmpName = $_FILES['userfile']['tmp_name'];

$filePath = $uploadDir . $fileName; // .../UPLOAD/NAME_OF_THE_FILE.JPG

$result = move_uploaded_file($tmpName, $filePath);

$title = $_POST['title']; //FROM PREVIOUS PAGE

$description = $_POST['description']; // FROM PREVIOUS PAGE

$date=date("y-m-d"); // GENERATED HERE


$img_title = $_POST['img_title'];

$img_type = $_POST['img_type'];

if (!$result) {

echo "Error uploading file";



$host="****"; // hostname

$username="****"; // username

$password="****"; // password

$db_name="davidmayhew01"; // database name

$tbl_name1="exhibinsert"; // table name

$tbl_name2="imgupload"; // table number two

//Connect to database

mysql_connect("$host", "$username", "$password")or die("Cannot connect to server"); 

mysql_select_db("$db_name")or die("Cannot connect to database");



$fileName = addslashes($fileName);

$filePath = addslashes($filePath);


//Add to database

$sql1="INSERT INTO $tbl_name1(title, description, date) VALUES('$title', '$description', '$date')";

$sql2="INSERT INTO $tbl_name2(name, img_title, img_type, img_date, path) 

	VALUES ('$fileName', '$img_title', '$img_type', '$img_date', '$filePath')"; //every new add = autoincrement number

mysql_query($sql1) or die('Error, query failed : ' . mysql_error());

mysql_query($sql2) or die('Error, query failed : ' . mysql_error());


echo "<br>Files uploaded<br>";

echo "$filePath";

echo "<br>";

echo "<img src='$filePath'>";

echo "<br>";

echo "<a href='index.php'>back</a>";



Open in new window

Question by:thephiller
  • 6
  • 5
LVL 19

Accepted Solution

Michael701 earned 500 total points
Comment Utility
Take a look at this snippet
// here's a sample to process muliple uploads


while (isset($_FILES['file'.$i))


  $this_file = $_FILES['file'.$i];

  // then precess this file


  // etc


// here's a sample to generate the html

echo "<form method='post' action='$_SERVER[PHP_SELF]' enctype='multipart/form-data'>\n";

for ($i = 1; $i <= $num_files; $i++) 


  echo "File $i: <input type='file' name='file". $i ."' /><br />\n";


echo "<input type='hidden' name='MAX_FILE_SIZE' value='$size_bytes' />\n";

echo "<input type='submit' name='submit' value='submit' />\n";

echo "</form>\n"; 

Open in new window


Author Comment

Comment Utility
Thanks for your reply.

Is there no way to already set a fixed number of fields instead of generating them with a for loop?
Also I've got the form on one page and then the upload script in another file (linked through the action field in the form). I'm guessing it won't work in the way that you posted it...

I'll give it a try and see if i can figure it out, I'm still a bit of a php noob ;)
LVL 19

Expert Comment

Comment Utility
sure you could write out the entire html for all 15 fields (just a  lot less code with the for loop)

it doesn't matter that you have the form action calling another script.

Author Comment

Comment Utility
Alright thanks, the for loop does sound better so I'll go for that.

And how about adding all entries into the database?
Do I have to create a table row for each of the pictures I'll be uploading?
Cos at the moment I have an ID field, Name field and a Path field (as well as some other fields but they aren't relevant to this question...). Do I need to create 10 path fields if I want to add 10 images? (ie. path1, path2, path3) or how does this work?

LVL 19

Expert Comment

Comment Utility
when php uploads files they are placed in a temp directory. most of the time you want to move them to a known directory (inside the website, if you are going  to display them to others). and, you want to rename them to a standard file name.

if (is_uploaded_file($_FILES['userfile']['tmp_name']))
  move_uploaded_file($_FILES['userfile']['tmp_name'], "/place/to/put/uploaded/filename.ext");
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!


Author Comment

Comment Utility
The script I'm using already (successfully) uploads one file to a folder and stores the path to the image in a field in my database.

My question is how this would work if I upload multiple files?
Obviously uploading multiple files to a folder is no problem, I'll just the for loop as you've described above but how do I store the paths to these images in my database.
At the moment I just have one path for one file but if I upload for example 10 files do I need 10 fields in my database? (one for each path of each image)

And if so how can I add each path into a different path field...?

Again, I appreciate your help so far, thanks!
LVL 19

Expert Comment

Comment Utility
Rather than create a fixed number of fields (10) for each record id. You'd be better creating another table with a one to many relationship, and store the path and image name here.

Here's an idea. How about always storing the photos in a fixed directory (/web/myweb/htdocs/photos/) then renaming the images to have to id as a prefix ie (0001_photo_a, 0001_photo_b, etc) Then, you can use the directory function to get all photos starting with the id_, This way you don't have to store the path or image name.

I do this on where the images are all prefixed with the item number. This way it doesn't matter how many images get uploaded for any individual item, the program just gets all images prefixed with the item number and displays them. Oh, most items only have 2 or 3 images, this one has 13.

Author Comment

Comment Utility
Thanks for the suggestion but (for now) I'd prefer to work with a database.

I am currently working with a one to many relationship inside my database. One "exhibition" contains 10 (more or less) images so I have a table that contains exhibitions and link it to a table with all the images (and image info).

Could you give me an example how I would go about entering the paths of each image into seperate database fields? (as I described above)


Author Comment

Comment Utility
Anyone got any suggestions?
LVL 19

Expert Comment

Comment Utility
if you have all separate fields then you'd have to make one big sql statement

$sql2="INSERT INTO $tbl_name2( ";

$sql2.= "name1, img_title1, img_type1, img_date1, path1, ";

$sql2.= "name2, img_title2, img_type2, img_date2, path2, ";

$sql2.= "name3, img_title3, img_type3, img_date3, path3, ";

$sql2.= "name4, img_title4, img_type4, img_date4, path4, ";

$sql2.= "name5, img_title5, img_type5, img_date5, path5, ";

// ...

$sql2.= "name10, img_title10, img_type10, img_date10, path10) values (";

$sql2.="'$fileName1', '$img_title1', '$img_type1', '$img_date1', '$filePath1', ";

$sql2.="'$fileName2', '$img_title2', '$img_type2', '$img_date2', '$filePath2', ";

$sql2.="'$fileName3', '$img_title3', '$img_type3', '$img_date3', '$filePath3', ";

$sql2.="'$fileName4', '$img_title4', '$img_type4', '$img_date4', '$filePath4', ";

$sql2.="'$fileName5', '$img_title5', '$img_type5', '$img_date5', '$filePath5', ";

// ...

$sql2.="'$fileName10', '$img_title10', '$img_type10', '$img_date10', '$filePath10');";

Open in new window


Author Comment

Comment Utility
Thanks a lot for all your help.

I chose your original reply as the solution since I did dig a little deeper to try and find out the possibilities of the code snipped you gave me and eventually got it to work exactly as I wanted.
So while eventually I didn't exactly follow through with my original questions your help was excellent, so thanks again! :)

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
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 …
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 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

5 Experts available now in Live!

Get 1:1 Help Now