943,925 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 990
  • PHP RSS
Jun 1st, 2008
0

displaying uploaded image

Expand Post »
Hi,

I am uploading images in a form. But it only sends the images in a folder 'upload' and send only the file name to the database. How do I send the image to the database and retrieve it for display.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
chitra1 is offline Offline
24 posts
since Dec 2007
Jun 1st, 2008
0

Re: displaying uploaded image

you are doing fine. Don't store the actual image in your database. To display you can do something like this:
php Syntax (Toggle Plain Text)
  1. <?php
  2. // just so we know it is broken
  3. error_reporting(E_ALL);
  4. // some basic sanity checks
  5. if(isset($_GET['image_id']) && is_numeric($_GET['image_id'])) {
  6. //connect to the db
  7. $link = mysql_connect("localhost", "username", "password") or die("Could not connect: " . mysql_error());
  8.  
  9. // select our database
  10. mysql_select_db("database_name") or die(mysql_error());
  11.  
  12. // get the image from the db
  13. $sql = "SELECT image FROM TABLE WHERE image_id=0";
  14.  
  15. // the result of the query
  16. $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
  17.  
  18. // set the header for the image
  19. header("Content-type: image/jpeg");
  20. echo mysql_result($result, 0);
  21.  
  22. // close the db link
  23. mysql_close($link);
  24. }
  25. else {
  26. echo 'Please use a real id number';
  27. }
  28. ?>
Vai
Reputation Points: 12
Solved Threads: 5
Junior Poster in Training
Vai is offline Offline
74 posts
since Jan 2008
Jun 2nd, 2008
0

Re: displaying uploaded image

Only think I will point out,

Is that the uploaded image when stored would have to be converted to jpeg for that to exclusively work. When working with files you have the option of giving the uploaded images specific filenames and then rendering them through the use of functions such as imagecreatefromjpeg and imagejpeg. What you might want to do instead of using files and expanding on the database idea, would be to store the mime-type, rather then returning each image as an apparent jpeg.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
iFuseDan is offline Offline
4 posts
since Jul 2006

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: Should I echo out password value in the password field?
Next Thread in PHP Forum Timeline: for loop inside if statement





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


Follow us on Twitter


© 2011 DaniWeb® LLC