PHP insert blob and update blob MySQL

Here is how to insert file BLOB MySQL.
PHP CODE:

if($_POST[submit]){
$file_name = $_FILES['file']['name'];
$tmp_name = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$fp = fopen($tmp_name, 'r');
$file_content = fread($fp, $file_size) or die("Error: cannot read file");
$file_content = mysql_real_escape_string($file_content) or die("Error: cannot read file");
fclose($fp);
// INSERT
$qu = "INSERT INTO `file`
(`file_content`,`file_name`,`file_type`,`file_size`)
VALUES
('$content','$file_name','$file_type','$file_size')";
$re = mysql_query($qu) or die ("Sorry Cant insert db!");
echo $file_name." inserted succesfully to database";
}
echo "< form method=\"post\" name=\"form1\" enctype=\"multipart/form-data\">";
echo " < input name=\"file\" type=\"file\">";
echo " < input name=\"submit\" type=\"submit\" value=\"Upload\">";
echo "< /form>";

3 comments:

Saud said...

is it that simple??

No dear its not i tried almost everything but am unable to store large data into the mysql database, it runs ok with the small ones.. but thats not something what BLOLB's are for. What if i had to store say 60 MB file or 100MB file into the database then.. please try for that and let me know in case u succeed i'll be grateful

Tsukassa said...

the blob type can hold up to 16MB of data, if you need something for 60-100MB files, you'll need a LONGBLOB type column

Tsukassa said...

Forgot to mention, a longblob type can hold up to 4GB of data.