Page 1 of 1

storing files in a database

Posted: Mon Aug 28, 2006 9:07 am
by Obadiah
does anyone know how i might go about doing this. ive seen another post that was relevant to the same thing but how would i actually go about doing it....does anyone know where i can possibly find some step by step info on this?

Posted: Mon Aug 28, 2006 9:09 am
by darodesign
If you use MySql select teh field type BLOB,
after that you can INSERT an complete File to the Database.
More information you can get on http://www.mysql.com
Read the Docs, for detailed informations!

An other way is to open the File and copy the content, and insert it into the Database.
But this works only for Text Files.

Posted: Mon Aug 28, 2006 9:29 am
by Obadiah
yea but arent those only for string or text types what if i wanted to store a picture or html,or another php file for that matter...would it work?

Posted: Mon Aug 28, 2006 9:32 am
by darodesign
Yes, BLOB can store files of all types ( i think ).
In stores byte code, so it shouldn't be a problem

Posted: Mon Aug 28, 2006 9:33 am
by Oren
Don't store files in the database. Instead, store the path to the file in the database.

Posted: Mon Aug 28, 2006 11:03 am
by pickle
Storing files in the database is definitely cool. Ultimately though, it's better to store the files in the filesystem like ~Oren said. If you're worried about security, you can always keep the files out of the web root & use a PHP file to serve up the file.

With that said, you can store pretty much anything you want in a database blob field. You need to send the right headers along with the binary data though, or you'll just get gibberish:

Code: Select all

$image_data;//let's say this contains the binary data that is your JPG image

header("Content-type: image/jpeg");
echo $image_data;
That snippet will successfully output an image.

I believe BLOB fields only hold 1.6MB of data though, so if you're going bigger than that, look at MEDIUMBLOB. BLOB stands for Binary Large OBject - basically you're just storing the binary data of ... something.

Posted: Mon Aug 28, 2006 3:51 pm
by feyd
TINYBLOB = 256 B
BLOB = 64 KB
MEDIUMBLOB = 16MB
LONGBLOB = 4GB

Same goes for the TEXT variants.