Wern Ancheta
Wern Ancheta

Reputation: 23337

How to save large files in mysql database

I'm trying to upload a 10Mb file in mysql database, the datatype of the field is LONGBLOB and I have already configured the max allowed packet in my.ini file but I still can't save the file. Files lower than 10Mb can be saved in the database so I'm wondering why it can't be saved. Are there any other places I need to check to get this to work?

max_allowed_packet = 1024M

This is how I get the data:

$data = $dbLink->real_escape_string(file_get_contents($_FILES['uploaded_file']['tmp_name']));

Upvotes: 1

Views: 3218

Answers (4)

Naveen Kumar
Naveen Kumar

Reputation: 4601

Don't do that

TEXT and BLOB is stored off the table with the table just having a pointer to the location of the actual storage.

When a table has TEXT or BLOB columns, the table can't be stored in memory. This means every query (which doesn't hit cache) has to access the file system - which is orders of magnitude slower than the memory.

Instead save the path of the file in DB.

Upvotes: 1

tereško
tereško

Reputation: 58454

While there are reasonable cases, like when storing small images ( though you can actually make similar functionality like ROLLBACK with filesystem too ), usually it is an exceptionally bad idea.

Files should be stored on the disk. And in case of large-scale systems: on a separate servers, dedicated for this purpose.

P.S.

You should stop using the outdated mysql_* functions. They are more then 10 years old and are not maintained anymore. Even the process for deprecation has begun. instead you should learn how to utilize prepared statements with either PDO or MySQLi.

Upvotes: 3

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76910

Don't do that, save the file on your server and than reference it in your db, otherwise you'll end up with a really slow db in no time

Upvotes: 5

Your Common Sense
Your Common Sense

Reputation: 158007

The best way would be not to save binary files in the database at all.

Upvotes: 3

Related Questions