943,833 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1616
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 4th, 2008
0

Image in database

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 2
Junior Poster
lordx78 is offline Offline
180 posts
since Oct 2007
Mar 4th, 2008
0

Re: Image in database

Hi...something like this may help get you pointed in the right direction
php Syntax (Toggle Plain Text)
  1. <?
  2. php require_once('/Connections/connect.php');
  3.  
  4. $colname_rsProject = "-1";
  5. if (isset($_GET['ID'])) {
  6. $colname_rsProjects = (get_magic_quotes_gpc()) ? $_GET['ID'] : addslashes($_GET['ID']);
  7. }
  8. mysql_select_db($database, $conn);
  9. $query_rsProject = sprintf("SELECT * FROM dbtable WHERE ID = %s", $colname_rsProject);
  10. $rsProject = mysql_query($query_rsProject, $conn) or die(mysql_error());
  11. $row_rsProject = mysql_fetch_assoc($rsProject);
  12. $totalRows_rsProject = mysql_num_rows($rsProject);
  13.  
  14.  
  15. define ('MAX_FILE_SIZE', 2048000);
  16. $AddData="No";
  17. $MM_flag="MM_update";
  18. if((isset($_POST["submit"])) && ($_POST["MM_update"] == "formname")) {
  19. $AddData="Yes";
  20. $startwarn="No";
  21. /// image/file upload code begin
  22.  
  23. if (array_key_exists('submit', $_POST)) {
  24. // define constant for upload folder
  25. define('UPLOAD_DIR', 'you upload directory info here');
  26. // replace any spaces in original filename with underscores
  27. // and assign to a simpler variable
  28. $file = str_replace(' ', '_', $_FILES['image']['name']);
  29. // convert the maximum size to KB
  30. $max = number_format(MAX_FILE_SIZE/1024, 1).'KB';
  31. // create an array of permitted MIME types
  32. $permitted = array('image/gif', 'image/jpeg', 'image/pjpeg', 'image/png');
  33. // begin by assuming the file is unacceptable
  34. $size_OK = false;
  35. $type_OK = false;
  36.  
  37. // check that file is within the permitted size
  38. if ($_FILES['image']['size'] > 0 && $_FILES['image']['size'] <= MAX_FILE_SIZE) {
  39. $size_OK = true;
  40. }
  41.  
  42. // check that file is of an permitted MIME type
  43. foreach ($permitted as $type) {
  44. if ($type == $_FILES['image']['type']) {
  45. $type_OK = true;
  46. break;
  47. }
  48. }
  49.  
  50.  
  51. if ($size_OK && $type_OK) {
  52. switch($_FILES['image']['error']) {
  53. case 0:
  54. // move the file to the upload folder and rename it
  55. $success_OK = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$file);
  56. if ($success_OK) {
  57. $result1 = "$file has been uploaded successfully";
  58. }
  59. else {
  60. $AddData = "No";
  61. $result = "There was an error uploading $file. Please try again.";
  62. }
  63. break;
  64. case 3:
  65. $AddData = "No";
  66. $result = "There was an error uploading $file. Please try again.";
  67. default:
  68. $AddData = "No";
  69. $result = "System error uploading $file. Please contact the webmaster.";
  70. }
  71. }
  72. elseif ($_FILES['image']['error'] == 4) {
  73.  
  74. $result1 = 'No file selected';
  75. }
  76. else {
  77. $AddData = "No";
  78. $result = "$file cannot be uploaded. Maximum size: $max. Acceptable file types: gif, jpg, png.";
  79. }
  80. }
  81.  
  82. if (isset($result)) {
  83. $AddData = "No";
  84. if ($startwarn=="No") { ?>
  85. <div class="startwarning">Please complete the missing item(s) indicated. </div>
  86. <?php $startwarn="Yes"; } ?>
  87. <div class="warning"><li><?php echo $result; ?></li></div>
  88.  
  89. <?php
  90. function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
  91. {
  92. $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;
  93.  
  94. switch ($theType) {
  95. case "text":
  96. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  97. break;
  98. case "long":
  99. case "int":
  100. $theValue = ($theValue != "") ? intval($theValue) : "NULL";
  101. break;
  102. case "double":
  103. $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
  104. break;
  105. case "date":
  106. $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
  107. break;
  108. case "defined":
  109. $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
  110. break;
  111. }
  112. return $theValue;
  113. }
  114.  
  115. $editFormAction = $_SERVER['PHP_SELF'];
  116. if (isset($_SERVER['QUERY_STRING'])) {
  117. $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
  118. }
  119.  
  120.  
  121. $ID = $_POST['ID'];
  122. mysql_select_db($database, $conn);
  123. $query_rsUploads = ("SELECT * FROM db WHERE ID = '$ID' and FilePath = '$file'");
  124. $rsUpload = mysql_query($query_rsUpload, $conn) or die(mysql_error());
  125. $row_rsUpload = mysql_fetch_assoc($rsUpload);
  126. $totalRows_rsUpload = mysql_num_rows($rsUpload);
  127.  
  128. if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "formname") && $AddData!="No" && $totalRows_rsUpload == 0 && $file !="") {
  129. $insert = sprintf("INSERT INTO dbtable (ID, FilePath) VALUES (%s, '$file')",
  130.  
  131. GetSQLValueString($_POST['ID'], "int"));
  132.  
  133. mysql_select_db($database, $conn);
  134. $query_rsUploadFile = sprintf("SELECT * FROM dbtable WHERE ID = %s", $colname_rsProject);
  135. $rsUploadFile = mysql_query($query_rsUploadFile, $conn) or die(mysql_error());
  136. $row_rsUploadFile = mysql_fetch_assoc($rsUploadFile);
  137. $totalRows_rsUploadFile = mysql_num_rows($rsUploadFile);
  138. ?>
  139.  
  140.  
  141. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  142. <html xmlns="http://www.w3.org/1999/xhtml">
  143. <head>
  144. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  145. <title>Untitled Document</title>
  146. <style type="text/css">
  147. <!--
  148. .warning {
  149. background-color: #ffffee;
  150. color: #660000;
  151. width: 650px;
  152. padding: 5px;
  153. border-right-width: thin;
  154. border-left-width: thin;
  155. border-right-style: solid;
  156. border-left-style: solid;
  157. border-right-color: #FF0000;
  158. border-left-color: #FF0000;
  159. text-indent: 20px;
  160. }
  161. .warningend {
  162. background-color: #ffffee;
  163. color: #660000;
  164. width: 650px;
  165. padding: 5px;
  166. border-right-width: thin;
  167. border-left-width: thin;
  168. border-right-style: solid;
  169. border-left-style: solid;
  170. border-right-color: #FF0000;
  171. border-left-color: #FF0000;
  172. border-bottom-width: thin;
  173. border-bottom-style: solid;
  174. border-bottom-color: #FF0000;
  175. }
  176. .startwarning {
  177. background-color: #ffffee;
  178. color: #660000;
  179. width: 650px;
  180. padding: 5px;
  181. border-right-width: thin;
  182. border-left-width: thin;
  183. border-right-style: solid;
  184. border-left-style: solid;
  185. border-right-color: #FF0000;
  186. border-left-color: #FF0000;
  187. border-top-width: thin;
  188. border-top-style: solid;
  189. border-top-color: #FF0000;
  190. }
  191. .notify {
  192. border: 1px solid #336699;
  193. background-color: #ffffee;
  194. color: #336699;
  195. width: 650px;
  196. padding: 5px;
  197. }
  198. -->
  199. </style>
  200.  
  201. </head>
  202.  
  203. <body>
  204. <form method="post" name="formname" action="<?php echo $editFormAction; ?>" enctype="multipart/form-data"/>
  205. <?php
  206. if (array_key_exists('submit', $_POST)) {
  207. }
  208. if($AddData=="no") {
  209. ?>
  210. <input type="hidden" name="ID" value="<?php echo $row_rsProject['ID']; ?>" />
  211. <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MAX_FILE_SIZE; ?>" />
  212. <input type="hidden" name="ID" value="<?php echo $row_rsProject['ID']; ?>">
  213. <input type="file" name="image" id="image" size="70" />
  214. <input type="submit" name="submit" value="Submit" />
  215. <input type="hidden" name="MM_update" value="formname">
  216. </form>
  217. <?php
  218. }
  219. ?>
  220. </body>
  221. </html>
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
JeniF is offline Offline
52 posts
since Aug 2007
Mar 4th, 2008
0

Re: Image in database

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 ...
Reputation Points: 10
Solved Threads: 15
Junior Poster in Training
silviuks is offline Offline
95 posts
since Apr 2006
Mar 4th, 2008
0

Re: Image in database

Quote ...
I wanted to how do we upload an image path(url) into the MySql database and call it through PHP.
When you upload an image, you upload it to a path. Store the path in a variable and at the time you upload, insert a record to the table containing the path.
Quote ...
Do we use the BLOB function, if yes, then how?
Nope. If you are uploading only the path, a fieldname of datatype varchar will do. eg. varchar(100).
Quote ...
I don't want to upload the image itself as it will burden the database.
Exactly ! Its not a good idea to upload images to the database.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Mar 5th, 2008
0

Re: Image in database

Quote ...
When you upload an image, you upload it to a path. Store the path in a variable and at the time you upload, insert a record to the table containing the path.
Can you show me an example of doing this process. thanks.
Reputation Points: 10
Solved Threads: 2
Junior Poster
lordx78 is offline Offline
180 posts
since Oct 2007
Mar 5th, 2008
0

Re: Image in database

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.
Moderator
Featured Poster
Reputation Points: 524
Solved Threads: 356
Purple hazed!
nav33n is offline Offline
3,878 posts
since Nov 2007
Mar 5th, 2008
0

Re: Image in database

I've visited the given url. But one question....

Quote ...
That's the path. Save that in the table.
How do I save it in the table. How does he path look like. Pls advise, still a bit blur on this part.
Reputation Points: 10
Solved Threads: 2
Junior Poster
lordx78 is offline Offline
180 posts
since Oct 2007
Mar 5th, 2008
0

Re: Image in database

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" ....
Reputation Points: 10
Solved Threads: 15
Junior Poster in Training
silviuks is offline Offline
95 posts
since Apr 2006
Mar 5th, 2008
0

Re: Image in database

yes, I've read it but just a bit confusing. Anyhow thanks for your guide.
Reputation Points: 10
Solved Threads: 2
Junior Poster
lordx78 is offline Offline
180 posts
since Oct 2007
Mar 5th, 2008
0

Re: Image in database

html Syntax (Toggle Plain Text)
  1. <form enctype="multipart/form-data" action="uploader.php" method="POST">
  2. <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
  3. Choose a file to upload: <input name="uploadedfile" type="file" /><br />
  4. <input type="submit" value="Upload File" />
  5. </form>
  6.  
  7. Can't we customize this input box, I mean the length of the text area, rename the browse button. Can we?
Reputation Points: 10
Solved Threads: 2
Junior Poster
lordx78 is offline Offline
180 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Contact form Flash/PHP sends with Text Formatting
Next Thread in PHP Forum Timeline: PHP Domxml help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC