Bummer! You have been redirected as the page you requested could not be found.

Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Take images from Mysql

HI,

I am trying to extract images from my database with php but the result is none. Here is my code.

<? if(isset($_GET['id'])) { // connect to the database include "../scrypts/db_connect.php";

// query the server for the picture $id = $_GET['id']; $query = "SELECT picture_name, picture_size, picture_type, picture_content FROM objects WHERE id = '$id'"; $result = mysql_query($query) or die(mysql_error());

// define results into variables $name=mysql_result($result,0,"picture_name"); $size=mysql_result($result,0,"picture_size"); $type=mysql_result($result,0,"picture_type"); $content=mysql_result($result,0,"picture_content");

// give our picture the proper headers...otherwise our page will be confused header("Content-Disposition: attachment; filename=$name"); header("Content-length: $size"); header("Content-type: $type"); echo $content; mysql_close(); }else{ die("No file ID given..."); }

?>

here are my table titles id name picture_name picture_type picture_size picture_content info_name info_type info_size info_content route uploaded

4 Answers

Have you checked the database to see if the image data is stored in the field?

MEDIUMBLOB can hold data up to 16Mb, if I'm not wrong, so unless you have a bigger image than that you should be ok with the field holding the full image.

You can use the following PHP snippet to save the images on the disk if you want.

if (isset($_FILES['itemUpload']['error']) && $_FILES['itemUpload']['error'] == 0 && preg_match('/image/i', $_FILES['itemUpload']['type'])) {
    move_uploaded_file($_FILES['itemUpload']['tmp_name'], $serverPath.$_FILES['itemUpload']['name']);
}

The code assumes the following:

  • you have a <input type="file" name="itemUpload" id="itemUpload">
  • the form has the * enctype="multipart/form-data"* attribute added.
  • the $serverPath variable should be set to the folder where you want to save the images in and also should contain a trailing slash. Ex: /var/www/test.com/images/
  • if you are running on a Linux system that folder should have write permissions so the image could be saved there.

Is the image stored in binary format in the database? What type is the picture_content field defined as?

As a side note, storing image data inside a table field is not a good idea, at least in my experience. Storing the image file name and retrieving the image from a folder inside your website would be a better approach.

There's another approach in which you can store the image data inside the table but base64_encode(d) data but it is also not a good idea since the image data will grow about 33% and when showing the image on the page the browser will not cache it.

Thanks for the fast response.

Well I am not sure how to upload picture in this forum. My picture_content is defined as mediumblob.

I too think that storing the image in folder is better however I need to be able to upload pictures from the website and later display them. My experience with web development is limited so I don't know any other way to upload pictures from the web page.

Thanks for the PHP code for saving on the disk. I will try that now. The picture is stored in the database i checked.