storing files in a database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

storing files in a database

Post 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?
User avatar
darodesign
Forum Newbie
Posts: 19
Joined: Mon Aug 28, 2006 8:58 am
Location: Berlin
Contact:

Post 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.
User avatar
Obadiah
Forum Regular
Posts: 580
Joined: Mon Jul 31, 2006 9:13 am
Location: Ashland, KY
Contact:

Post 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?
User avatar
darodesign
Forum Newbie
Posts: 19
Joined: Mon Aug 28, 2006 8:58 am
Location: Berlin
Contact:

Post by darodesign »

Yes, BLOB can store files of all types ( i think ).
In stores byte code, so it shouldn't be a problem
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Don't store files in the database. Instead, store the path to the file in the database.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

TINYBLOB = 256 B
BLOB = 64 KB
MEDIUMBLOB = 16MB
LONGBLOB = 4GB

Same goes for the TEXT variants.
Post Reply