CodeCharge Studio
search Register Login  

Web Reports

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> Archive -> GotoCode Archive

 PHP uploading multiple images into MySQL

Print topic Send  topic

Author Message
Rick Page
Posted: 09/11/2002, 9:13 AM

Hello everyone,
I know that I'm not the only one looking for this feature. Here is what I've come up with. If anyone has a different solution, please share it.

Images are saved as a blob in MySQL db.

This assumes that you have already created an entry in the db and just need to update it with an image.
Here is my form:
"<form method=\"post\" action=\"UploadDB.php\" enctype=\"multipart/form-data\">
<INPUT TYPE=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"1000000\">
<input type=\"file\" name=\"myPhoto[]\" size=\"20\"><br>
<input type=\"file\" name=\"myPhoto[]\" size=\"20\"><br>
<input type=\"file\" name=\"myPhoto[]\" size=\"20\"><br>
<input type=\"file\" name=\"myPhoto[]\" size=\"20\"><br>
<input type=\"file\" name=\"myPhoto[]\" size=\"20\"><br>
<INPUT TYPE=\"hidden\" NAME=\"photo_id\" VALUE=\"".$fldphoto_id."\"> //This is the unique id
<input type=\"submit\" name=\"submit\" value=\"Add Photos\">
</form>

Here is part of my UploadDB.php, this is placed in the open event:
$photo_id = toSQL(get_param("photo_id"), "Number");
// Here I'm copying the other fields in the record I'm adding the first photo to. I will use this copy information when creating new records for each of the additional images.
$client_id = dlookup("photos", "client_id", "photo_id=".$photo_id);
$category_id = dlookup("photos", "category_id", "photo_id=".$photo_id);
$photo_date = dlookup("photos", "photo_date", "photo_id=".$photo_id);
$comments = dlookup("photos", "comments", "photo_id=".$photo_id);

$count = 0;
while($myPhoto[$count] != "none") {
$data = addslashes(fread(fopen($myPhoto[$count], "r"), filesize($myPhoto[$count])));
$imageInfo = GetImageSize($myPhoto[$count]); //Get dimensions of image
$myTitle = explode(".", $_FILES['myPhoto']['name'][$count]);
$gif = eregi("\.gif", $_FILES['myPhoto']['name'][$count]);
$jpg = eregi("\.jpg", $_FILES['myPhoto']['name'][$count]);
$png = eregi("\.png", $_FILES['myPhoto']['name'][$count]);
$file_ext = $gif + $jpg + $png;
$file_label = "gif, jpg or png";
$max_size = 100000;
$temp_size = $_FILES['myPhoto']['size'][$count];
$temp_type = $_FILES['myPhoto']['type'][$count];

echo "Uploading Photo... <br>";

if($temp_size > $max_size) {
echo "<br>Your image file size (".$temp_size.") for ".$MyTitle." is greater than ".$max_size.".<br> Please optimize image and try again.";
}
else if(!$file_ext) {
echo "<br>Your file (".$MyFile.") is not of type ".$file_label.".<br> Please go back and try again. ";
}
else {
if ($count == 0) { //first record
$uSQL = "update photos set photo_title='$myTitle[0]' where photo_id=".$photo_id;
$db->query($uSQL);
$uSQL = "update photos set photo_blob='$data' where photo_id=".$photo_id;
$db->query($uSQL);
$uSQL = "update photos set photo_type='$temp_type' where photo_id=".$photo_id;
$db->query($uSQL);
$uSQL = "update photos set photo_size='$temp_size' where photo_id=".$photo_id;
$db->query($uSQL);
$uSQL = "update photos set photo_width='$imageInfo[0]' where photo_id=".$photo_id;
$db->query($uSQL);
$uSQL = "update photos set photo_height='$imageInfo[1]' where photo_id=".$photo_id;
$db->query($uSQL);
}
else {
if ($count > 0) { // Not first image, need to create new record with info copied earlier
$iSQL = "insert into photos (client_id, category_id, photo_date, comments) values (" .
tosql($client_id, "Number") . "," .
tosql($category_id, "Number") . "," .
tosql($photo_date, "Date") . "," .
tosql($comments, "Memo") .
")";
}
$db->query($iSQL);
$photo_id = mysql_insert_id();
$uSQL = "update photos set photo_title='$myTitle[0]' where photo_id=".$photo_id;
$db->query($uSQL);
$uSQL = "update photos set photo_blob='$data' where photo_id=".$photo_id;
$db->query($uSQL);
$uSQL = "update photos set photo_type='$temp_type' where photo_id=".$photo_id;
$db->query($uSQL);
$uSQL = "update photos set photo_size='$temp_size' where photo_id=".$photo_id;
$db->query($uSQL);
$uSQL = "update photos set photo_width='$imageInfo[0]' where photo_id=".$photo_id;
$db->query($uSQL);
$uSQL = "update photos set photo_height='$imageInfo[1]' where photo_id=".$photo_id;
$db->query($uSQL);

// verified that image is saved
$success = dlookup("photos","photo_blob","photo_id=".$photo_id);
if ($success!=null) { //Display image
echo "Done.<br>Your photo was successfully upload<br><img src=\"showPhoto.php?photo_id=".$photo_id."&thumb=0&quirk=".time()."\">";
}
else {
echo "<br>Photo upload failed, please go back and try again.<br>";
}
}
$count++;
}
echo "<a href=\"javascript:history.back()\"> Back </a>";


I hope this is helpful. I know there are problably better ways at doing this. I would be interested in knowing what they are.

Rick Page
Rick Page
Posted: 09/11/2002, 9:46 AM

Sorry, I notice an error in copying code over.

if ($count > 0) {
$iSQL = "insert into photos (client_id, category_id, photo_date, comments) values (" .
tosql($client_id, "Number") . "," .
tosql($category_id, "Number") . "," .
tosql($photo_date, "Date") . "," .
tosql($comments, "Memo") .
")";
$db->query($iSQL); // this is in correct location
$photo_id = mysql_insert_id(); // this is in correct location
} In my last post, I had placed the two above lines after this brace.

Rick Page

   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

MS Access to Web

Convert MS Access to Web.
Join thousands of Web developers who build Web applications with minimal coding.

CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.