Binary Data Store
Binary Data Store
If you want to store binary data like images and html files directly in
your MySQL database, this column is for you!
I will show how you can store the data via the HTML forms "File"
feature in your database and how you can access and use this data in
your webproject.
If you have read the article "PHP, MySQL and images" by William
Samplonius here on phpbuilder.com, this might be interesting for you
as William stores the binary data somewhere on your harddisk (using
a shell command), instead of storing the image directly in the SqlDatabase.
Overview:
First of all, you have to create a new database on your SQL server in
which your script will store the binary data.
For my example I use the following structure. To create this database,
you have to do the following steps:
CREATETABLEbinary_data(
idINT(4)NOTNULLAUTO_INCREMENTPRIMARYKEY,
descriptionCHAR(50),
bin_dataLONGBLOB,
filenameCHAR(50),
filesizeCHAR(50),
filetypeCHAR(50)
);
A sample php3 script you can use to store data in your database
With the php script store.php3 you can transfer files via a html form
interface into the created database.
store.php3
<?php
//store.php3byFlorianDittmer<dittmer@gmx.net>
//Examplephpscripttodemonstratethestoringof
binaryfilesinto
//ansqldatabase.Moreinformationcanbefoundat
http://www.phpbuilder.com/
?>
<html>
<head><title>StorebinarydataintoSQL
Database</title></head>
<body>
<?php
//codethatwillbeexecutediftheformhasbeen
submitted:
if($submit){
//connecttothedatabase
//(youmayhavetoadjustthehostname,usernameor
password)
MYSQL_CONNECT("localhost","root","password");
mysql_select_db("binary_data");
$data=addslashes(fread(fopen($form_data,"r"),
filesize($form_data)));
$result=MYSQL_QUERY("INSERTINTObinary_data
(description,bin_data,filename,filesize,filetype)".
"VALUES
('$form_description','$data','$form_data_name','$form_dat
a_size','$form_data_type')");
$id=mysql_insert_id();
print"<p>ThisfilehasthefollowingDatabaseID:
<b>$id</b>";
MYSQL_CLOSE();
}else{
//elseshowtheformtosubmitnewdata:
?>
<formmethod="post"action="<?phpecho$PHP_SELF;?>"
enctype="multipart/formdata">
FileDescription:<br>
<inputtype="text"
name="form_description"size="40">
<inputtype="hidden"name="MAX_FILE_SIZE"
value="1000000">
<br>Filetoupload/storeindatabase:<br>
<inputtype="file"name="form_data"size="40">
<p><inputtype="submit"name="submit"value="submit">
</form>
<?php
}
?>
</body>
</html>
So if you execute this script, you will see a simple html form. Use the
"browse" button to select a file (for example: an image) and press the
"submit" button.
After the file has been uploaded to the webserver, the script will tell
you which database ID the uploaded file has. You need to know this ID
to access this data (with the following script).
The script getdata.php3 is an example script that fetches the binary
data from the database and passes it directly to the user.
<?php
//getdata.php3byFlorianDittmer<dittmer@gmx.net>
//Examplephpscripttodemonstratethedirectpassing
ofbinarydata
//totheuser.Moreinfosathttp://www.phpbuilder.com
//Syntax:getdata.php3?id=<id>
if($id){
//youmayhavetomodifylogininformationforyour
databaseserver:
@MYSQL_CONNECT("localhost","root","password");
@mysql_select_db("binary_data");
$query="selectbin_data,filetypefrombinary_data
whereid=$id";
$result=@MYSQL_QUERY($query);
$data=@MYSQL_RESULT($result,0,"bin_data");
$type=@MYSQL_RESULT($result,0,"filetype");
Header("Contenttype:$type");
echo$data;
};
?>
As the script needs to "know" which file is
requested, you have to add the ID as a
parameter.
Example: A file has been stored with ID 2
in the database. To get this file, you have
to call:
getdata.php3?id=2
If you have images saved in the database,
you can use the getdata script as <img
src> in your webpage.
Example: You saved an Image as ID 3 in
the database and want to show it on your
webpage. Use the following code:
<imgsrc="getdata.php3?id=3">
[ Next Page ]
If you are using Unix, check out your inittree and change the corresponding startup
file.
I hope this works fine for all of you. I also
want to thank those of you, who wrote in