Tutorial details
Name : Retrieve and display blob using MySQL
Author : Kenneth Clark
Language : PHP 4
Platform : Platform Independant
Click here if you require development or hosting solutions : [ SkyeTech Solutions ]

Alot of people like to store links to images inside the database and store the physical file on the OS.

This is all fine and well but can be a nightmare to maintain. Imagine you have a script that deletes files when a record is deleted from the database. Now what if this script started failing but because you don't physically monitor it you don't notice it failing. The only time you will notice is when you run out of disk space. Then you have to write a script to check the db and remove the files and you pray that you don't kill a referenced file. Very, very nasty

The answer? Store the image inside the database. Then when the record gets delete you have to, well, do nothing. The image is gone as well

What follows is a very simple description of how to do this in PHP 4, this tutorial assumes you know how to connect to a database.

Firstly we need to get the data from our form. The HTML form to upload data, in it's bare bones form, might look something like

< form method="post" action="uploadImage.php"
 enctype="multipart/form-data">
< input type="file" name="image" />
< input type="submit" value=" Upload " />
< /form>
This is as basic as it gets folks. this form will submit the file selected to the file uploadImage.php

Right now for uploadImage.php

$upload= (empty($_FILES['image'])) ? 0 : $_FILES['image'];
$fileName = $upload['tmp_name'];
$fType = $upload['type'];
$size = $upload['size'];
$timer = $size * 1024;
set_time_limit($timer);
Now as you see above the tmp_name index contains the temporary file name for the file. The index type contains the MIME type of the file (using this index you can do your checking to make sure that only image files go up). The set_time_limit function I call just to make sure the script doesn't time out. You don't need to do this.

Right, next up we are going to read the file and insert it into a table. The table strucuture will look something like:

[ImageTable]
id (bigint, primary key)
imageData (blob)
imageType (varchar(50))

You can add things like description etc but this is just
a simple example.
Now that we have the table we can put the data into it, which is very simple.
$open = fopen($fileName,"r");
$data_read = fread($open, $size);
fclose($open);
$query = "INSERT INTO ImageTable(imageData,imageType)
          VALUES(\"" . addslashes($data_read) . "\",
                 \"" . mysql_escape_string($fType) . "\");";
// execute your query against the database here.
// Connecting to a MySql Database
Now that it is stored inside the database you might be asking "how on earth do I retrieve it?". Well it is as simple as inserting it into the database.

< img src="images.php?id=2" />


//PHP code (for images.php)
$id = $_GET['id'];
$query = "SELECT * FROM ImageTable WHERE id = " . $id;
//database connection routine
// Connecting to a MySql Database
$result = mysql_query($query);
if($row = mysql_fetch_array($result)){
  $imageData = $row['imageData'];
  $imageType = $row['imageType'];
}
header("Content-type: ". $imageType);
echo $imageData;
And that, as they say, is that. This is not production quality code, please remember to sanitize any posted information that ends up in an SQL query, check the file type being upload and limit the size of the file. You might als look at writing a resizing routine for automatically creating thumbnails. In the mean time here is a simple function to do that (Generating a thumbnail). I wil post a tutorial on doing that soon.


SkyeTX Technologies Business Software Solutions
SkyeTX Technologies Business Software Solutions