2

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?

Valentoni
  • 308
  • 4
  • 19
  • 2
    Even though it might not be the source of the error, it's good practice to also set the content size header: `header("Content-length: $filedata['size']");`. Anyway, how do you store the file data in the database? Is everything properly encoded? – Paul Feb 25 '16 at 21:05
  • Yup! It comes from a `mysqli_real_escape_string($mysql_link, get_file_contents($_FILES['file']['tmp_name']));` The result of this execution is the value stored in the database BLOB field. – Valentoni Feb 25 '16 at 21:13
  • 1
    Despite favouring PDO over mysqli, I can't see anything really wrong. Additionally to `Pragma`, I would also set `header("Cache-Control: no-cache");` and the `Content-length` mentioned above. Only thing I could think of would be some data limitation. In MySQL you are using MEDIUMBLOB, I assume (which should be fine). – Paul Feb 25 '16 at 21:25
  • I used `header("Cache-Control: no-cache");` Same result. – Valentoni Feb 25 '16 at 21:39
  • Did you try different PDF files from different sources (Acrobat, Indesign, PDF printer, MS Office, ...)? Are they all "broken" after being stored in the database? – Paul Feb 25 '16 at 21:45
  • @Paul I had tried with 2 PDF. Both didn't work after download. But after read your comment i ran a test: set some working PDF files from my local machine, uploaded all, then downloaded all back. Some of the downloaded files worked, some of them didn't. – Valentoni Feb 25 '16 at 22:37
  • 1
    Well, than it seems to be an error with the PDF file format for some files. Your system apparently is ok, I guess there's not much you can do except saving the files as files and put only the path to the file into the database. – Paul Feb 26 '16 at 09:32
  • But it happens with some other file types, as zip, for example. Weird! Thank you for your help, anyway! – Valentoni Feb 26 '16 at 11:06
  • Maybe the filesize? Check this out: http://stackoverflow.com/a/6766854/480852 With type blob, the maximum stored is 64kb, if is medium blob you can put up to 16mb, and so on. – Lucas Serafim Feb 26 '16 at 17:30
  • Hmm interesting! But wouldn't this throw a mysql error? I mean: this way the file would not be stored in the field, at all, and the query would fail, wouldn't it? Thx! – Valentoni Feb 26 '16 at 18:47

0 Answers0