| | |
Image in database
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
I wanted to how do we upload an image path(url) into the MySql database and call it through PHP. Do we use the BLOB function, if yes, then how? I don't want to upload the image itself as it will burden the database.
Please Advise.*
* Any Tutorials, videos, article, books......pls include the links
Thanks.
Please Advise.*
* Any Tutorials, videos, article, books......pls include the links
Thanks.
"I might not be the BEST but I'm not like the REST!"
Hi...something like this may help get you pointed in the right direction
php Syntax (Toggle Plain Text)
<? php require_once('/Connections/connect.php'); $colname_rsProject = "-1"; if (isset($_GET['ID'])) { $colname_rsProjects = (get_magic_quotes_gpc()) ? $_GET['ID'] : addslashes($_GET['ID']); } mysql_select_db($database, $conn); $query_rsProject = sprintf("SELECT * FROM dbtable WHERE ID = %s", $colname_rsProject); $rsProject = mysql_query($query_rsProject, $conn) or die(mysql_error()); $row_rsProject = mysql_fetch_assoc($rsProject); $totalRows_rsProject = mysql_num_rows($rsProject); define ('MAX_FILE_SIZE', 2048000); $AddData="No"; $MM_flag="MM_update"; if((isset($_POST["submit"])) && ($_POST["MM_update"] == "formname")) { $AddData="Yes"; $startwarn="No"; /// image/file upload code begin if (array_key_exists('submit', $_POST)) { // define constant for upload folder define('UPLOAD_DIR', 'you upload directory info here'); // replace any spaces in original filename with underscores // and assign to a simpler variable $file = str_replace(' ', '_', $_FILES['image']['name']); // convert the maximum size to KB $max = number_format(MAX_FILE_SIZE/1024, 1).'KB'; // create an array of permitted MIME types $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png'); // begin by assuming the file is unacceptable $size_OK = false; $type_OK = false; // check that file is within the permitted size if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) { $size_OK = true; } // check that file is of an permitted MIME type foreach ($permitted as $type) { if ($type == $_FILES['image']['type']) { $type_OK = true; break; } } if ($size_OK && $type_OK) { switch($_FILES['image']['error']) { case 0: // move the file to the upload folder and rename it $success_OK = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$file); if ($success_OK) { $result1 = "$file has been uploaded successfully"; } else { $AddData = "No"; $result = "There was an error uploading $file. Please try again."; } break; case 3: $AddData = "No"; $result = "There was an error uploading $file. Please try again."; default: $AddData = "No"; $result = "System error uploading $file. Please contact the webmaster."; } } elseif ($_FILES['image']['error'] == 4) { $result1 = 'No file selected'; } else { $AddData = "No"; $result = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png."; } } if (isset($result)) { $AddData = "No"; if ($startwarn=="No") { ?> <div class="startwarning">Please complete the missing item(s) indicated. </div> <?php $startwarn="Yes"; } ?> <div class="warning"><li><?php echo $result; ?></li></div> <?php function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue; switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } $ID = $_POST['ID']; mysql_select_db($database, $conn); $query_rsUploads = ("SELECT * FROM db WHERE ID = '$ID' and FilePath = '$file'"); $rsUpload = mysql_query($query_rsUpload, $conn) or die(mysql_error()); $row_rsUpload = mysql_fetch_assoc($rsUpload); $totalRows_rsUpload = mysql_num_rows($rsUpload); if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "formname") && $AddData!="No" && $totalRows_rsUpload == 0 && $file !="") { $insert = sprintf("INSERT INTO dbtable (ID, FilePath) VALUES (%s, '$file')", GetSQLValueString($_POST['ID'], "int")); mysql_select_db($database, $conn); $query_rsUploadFile = sprintf("SELECT * FROM dbtable WHERE ID = %s", $colname_rsProject); $rsUploadFile = mysql_query($query_rsUploadFile, $conn) or die(mysql_error()); $row_rsUploadFile = mysql_fetch_assoc($rsUploadFile); $totalRows_rsUploadFile = mysql_num_rows($rsUploadFile); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title> <style type="text/css"> <!-- .warning { background-color: #ffffee; color: #660000; width: 650px; padding: 5px; border-right-width: thin; border-left-width: thin; border-right-style: solid; border-left-style: solid; border-right-color: #FF0000; border-left-color: #FF0000; text-indent: 20px; } .warningend { background-color: #ffffee; color: #660000; width: 650px; padding: 5px; border-right-width: thin; border-left-width: thin; border-right-style: solid; border-left-style: solid; border-right-color: #FF0000; border-left-color: #FF0000; border-bottom-width: thin; border-bottom-style: solid; border-bottom-color: #FF0000; } .startwarning { background-color: #ffffee; color: #660000; width: 650px; padding: 5px; border-right-width: thin; border-left-width: thin; border-right-style: solid; border-left-style: solid; border-right-color: #FF0000; border-left-color: #FF0000; border-top-width: thin; border-top-style: solid; border-top-color: #FF0000; } .notify { border: 1px solid #336699; background-color: #ffffee; color: #336699; width: 650px; padding: 5px; } --> </style> </head> <body> <form method="post" name="formname" action="<?php echo $editFormAction; ?>" enctype="multipart/form-data"/> <?php if (array_key_exists('submit', $_POST)) { } if($AddData=="no") { ?> <input type="hidden" name="ID" value="<?php echo $row_rsProject['ID']; ?>" /> <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" /> <input type="hidden" name="ID" value="<?php echo $row_rsProject['ID']; ?>"> <input type="file" name="image" id="image" size="70" /> <input type="submit" name="submit" value="Submit" /> <input type="hidden" name="MM_update" value="formname"> </form> <?php } ?> </body> </html>
I keep hitting "escape", but I'm still here!!!!
:}
:}
•
•
Join Date: Apr 2006
Posts: 66
Reputation:
Solved Threads: 11
If you know a little bit PHP and MySQL there is no problem to accomplish your task. in your database should store only the file name (let say column name is image_name). you must define a constant with your upload folder something like: define("UPLOAD_DIR", "images_uploaded"); . when you will display the images you will refer as follows:
// do whatever (db) processing is necessarily
while ($obj = mysql_fetch_object($rez))
{
echo "<img src='./".UPLOAD_DIR."/".$obj->image_name."' alt='some alt desc'>";
}
hope it's useful ...
// do whatever (db) processing is necessarily
while ($obj = mysql_fetch_object($rez))
{
echo "<img src='./".UPLOAD_DIR."/".$obj->image_name."' alt='some alt desc'>";
}
hope it's useful ...
•
•
•
•
I wanted to how do we upload an image path(url) into the MySql database and call it through PHP.
•
•
•
•
Do we use the BLOB function, if yes, then how?
•
•
•
•
I don't want to upload the image itself as it will burden the database.
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
http://www.tizag.com/phpT/fileupload.php In this example, in move_uploaded_file function, look at $target_path. That's the path. Save that in the table.
Ignorance is definitely not bliss!
*PM asking for help will be ignored*
*PM asking for help will be ignored*
•
•
Join Date: Apr 2006
Posts: 66
Reputation:
Solved Threads: 11
I don't know if you read (or understand) my previous post....
in the database you should keep only the file name. let say your file is image1.jpg and it is stored in {root}/images/uploaded_images/ + image1.jpg. why i think it's a better idea to keep only the filename? think what will happen if you store something like: "images/uploaded_images/image1.jpg" and one day you decide to rename your folder from "uploaded_images" to "uploads" ....
in the database you should keep only the file name. let say your file is image1.jpg and it is stored in {root}/images/uploaded_images/ + image1.jpg. why i think it's a better idea to keep only the filename? think what will happen if you store something like: "images/uploaded_images/image1.jpg" and one day you decide to rename your folder from "uploaded_images" to "uploads" ....
html Syntax (Toggle Plain Text)
<form enctype="multipart/form-data" action="uploader.php" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> Choose a file to upload: <input name="uploadedfile" type="file" /><br /> <input type="submit" value="Upload File" /> </form> Can't we customize this input box, I mean the length of the text area, rename the browse button. Can we?
"I might not be the BEST but I'm not like the REST!"
![]() |
Similar Threads
- displaying image from database (PHP)
- how to save image into database (VB.NET)
- retieve image from database (ASP.NET)
- Download image from DB and view it... (C#)
- uploading image in database (PHP)
Other Threads in the PHP Forum
- Previous Thread: Contact form Flash/PHP sends with Text Formatting
- Next Thread: updating records using php and mysql
| Thread Tools | Search this Thread |
apache api array autocomplete beginner binary body broken cakephp class cms code cron curl database dataentry date date/time display duplicates dynamic ebooks email emptydisplayvalue error execute explodefunction file firstoptioninphpdroplist folder form forms function functions google href htaccess html image include ip javasciptvalidation javascript joomla keywords limit link list login matching mediawiki menu msqli_multi_query multiple mycodeisbad mysql navigation number oop parameter paypal pdf php phpincludeissue query random recourse recursive regex script search seo server sessions shot source sp space speed sql static subdomain subscription system table tag tutorial tutorials update upload url validator variable vbulletin video web webdesign white xml youtube






