I have a blob field in a database. In which i store values resulting from this:
mysqli_real_escape_string($mysql_link, file_get_contents($_FILES['file']['tmp_name']));
I created a script to download this file from database.
The script works and the file is downloaded. But there is an issue: If the file stored there, is an image file, like "jpeg" or "png", everything is just fine. But if it is other type, as "PDF", for example, when i open it, the pdf reader can't read the file. Says that the pdf is damaged. I don't know why it's happening with some file types and not with others.
Here's the download script:
function download($filedata, $filename){
header('Content-Type: '.$filedata['mime']);
header('Content-Disposition: attachment; filename="' . $filename . "." . $filedata['extension']);
header('Pragma: no-cache');
ob_clean();
echo $filedata['data'];
exit;
}
$filedata
is the fetched result from database. I also checked it and the values are correct, according to each file, and after all, the images are working perfectly.
It is really tricking me!
Assuming this PDF example execution, the var values would be as follows:
$filedata['mime'] = "application/pdf";
$filedata['extension'] = "pdf";
$filedata['data'] = the blob content;
$filename = uniqid();
Thx in advance!
UPDATE:
I ran a test with PDF files. Selected a set of working PDF's from my local machine, uploaded all, storing them in the database, using the file_get_contents()
mechanism, cited above, then downloaded it all back, using the download script, also cited above. The results: some of the downloaded PDF files worked, some of them didn't.
A friend told me something about the encoding of the files. Which could explain the fact that some files, after download, works and some do not. Could it be? And how could i fix it?
UPDATE:
I used echo mb_detect_encoding($filedata['data']); exit;
and all printed "UTF-8", for both working and not working files. So, it's not an encoding issue. Any other ideas?