Trouble uploading multiple images with this script.

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

Who is Participating?
Michael701Connect With a Mentor Commented:
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

thephillerAuthor Commented:
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 ;)
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.
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

thephillerAuthor Commented:
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?

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");
thephillerAuthor Commented:
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!
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.
thephillerAuthor Commented:
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)

thephillerAuthor Commented:
Anyone got any suggestions?
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

thephillerAuthor Commented:
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! :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.