Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • Last Modified:

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?
Like:
id
path1
path2
path3
etc.

??


Here's the script that uploads the image:



<?php
$uploadDir = "upload/";
 
if(isset($_POST['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
 
//EXTRACT FROM NEW-EXHIBITION2.PHP
$img_title = $_POST['img_title'];
$img_type = $_POST['img_type'];
 
 
if (!$result) {
echo "Error uploading file";
exit;
}
 
 
 
 
$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");
 
if(!get_magic_quotes_gpc())
{
$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());
 
mysql_close();
 
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

0
thephiller
Asked:
thephiller
  • 6
  • 5
1 Solution
 
Michael701Commented:
Take a look at this snippet
// here's a sample to process muliple uploads
$i=1;
while (isset($_FILES['file'.$i))
{
  $this_file = $_FILES['file'.$i];
  // then precess this file
  $fileName=$this_file['name'];
  // 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

0
 
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 ;)
0
 
Michael701Commented:
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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?


0
 
Michael701Commented:
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");
0
 
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!
0
 
Michael701Commented:
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 http://www.clarkgravesantiques.com/item_detail.php?item=512F03 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.
0
 
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)

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

0
 
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! :)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now