Reputation: 202
I have a MySQL table in which some data blob data is stored in a cell.
I tried to echo it.
But I don't know about the mime type of that data. How do I decide the mime type from the given blob data?
I tried this code but don't know about the mime type of blob:
<?php
mysql_connect("localhost","root","");
mysql_select_db("contents");
$sql="SELECT * FROM `contents` limit 1";
$result=mysql_query($sql);
$row=mysql_fetch_assoc($result);
echo $row['html']
?>
Upvotes: 1
Views: 14385
Reputation: 2999
This is an old question but it is the first one in the search results
To get the mime type of a binary string you can use finfo_buffer (or the finfo class) http://php.net/manual/fr/function.finfo-buffer.php
// Object oriented style
$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->buffer($buffer);
// Procedural style
$finfo = finfo_open(FILEINFO_MIME);
$mime = finfo_buffer($finfo, $buffer)
Upvotes: 2
Reputation: 4168
You must add mime with insert "blob" in db for if you force ,you can choose this way :
if (!function_exists('mime_content_type ')) {
function mime_content_type($filename) {
$finfo = finfo_open(FILEINFO_MIME);
$mimetype = finfo_file($finfo, $filename);
finfo_close($finfo);
return $mimetype;
}
}
$filename = tempnam('/tmp', 'cre');
$fp = fopen($filename, 'w');
fwrite($fp, $row['html']);
fclose($fp);
$ctype = mime_content_type($filename) ;
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".'samename'."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
unlink($filename);
echo $row['html'];
This way is not good , because have many usage , but is possible. I offer you update table and add mime-type and edit all record by top code ...
And you have second way and use :
header("Content-type: application/force-download");
This is force download header and can save files in user's PC.
Upvotes: 4
Reputation: 5022
You create huge overhead in doing this. You should store the MIME type on insert as the other user suggested.
Do not use the MIME type received from a form upload as this cannot be trusted (it is easily faked by a client), but calculate this yourself after the file is uploaded.
If you already have a table full of files in BLOBs and no MIME types, but you know you'll want to refer to these regularly, I recommend adding the MIME column and writing a script to populate the MIME field with all the values by looping through existing records and adding this.
Hence refer to the MIME field for the type instead of loading all the BLOB out into memory to parse the MIME type.
Upvotes: 0