-1

I have a problem downloading the Blob file from MYSQL using php.

Here is my php script in downloading file:

        $querysel = "Select * from file where file_id = '$fileid'";
        $resultsel = mysql_query($querysel);
        $rowsel = mysql_fetch_array($resultsel);

        $filename = $rowsel['file_name'];
        $mimetype = $rowsel['mime_type'];
        $filesize = $rowsel['file_size'];

        header("Content-length: ".$filesize);
        header("Content-type: ".$mimetype);
        header("Content-disposition: attachment; filename=".$filename);
        header("Content-Description: PHP Generated Data");
        header("Content-transfer-encoding: binary");
        echo $filename;

And this table in mysql:enter image description here

PROBLEM   [UPDATED]

  1. What seems to be the problem here is that after downloading the file, the file size from that particular file don't match with the file size saved in mysql. I don't know why?

  2. I also encounter this small problem, After I downloaded the Powerpoint and delete it and download it again would be automatically saved to downloads and will automatically be opened. Is that normal?

Example:

The BLOB file with 6.8MB of file size was supposed to be downloaded with 6.8MB also but what happened here is that the file size of the downloaded file is only 25bytes. Why?

Any help would be much appreciated. Thanks

Makudex
  • 1,042
  • 4
  • 15
  • 40
  • Why don't you try this? http://www.2my4edge.com/2013/07/file-download-coding-using-php-and-mysql.html – Amit Horakeri Sep 14 '15 at 14:43
  • use unserialize on file_content column : http://php.net/manual/fr/function.unserialize.php And after check file size – Fky Sep 14 '15 at 14:44
  • 3
    I think you'll find "01 LCD Slides 1.pps" is about 25 chars. Maybe you wanted to return the file, not the filename? – markdwhite Sep 14 '15 at 14:44
  • You are doing `echo $filename;`. All you are doing is making a text file with the string "01 LCD Slides 1.pps". – gen_Eric Sep 14 '15 at 14:44
  • 2
    last line: `echo $filename` do not send content but only a filename (probably 25bytes long?) – jasir Sep 14 '15 at 14:44
  • 1
    P.S. It's usually suggested that you upload files to the filesystem, *not* the database. You can store the filename, path, and metadata in the db, but it's usually not suggested to store the file in the db. – gen_Eric Sep 14 '15 at 14:45
  • one more reason not to put files into a db – e4c5 Sep 14 '15 at 14:45
  • 1
    `BLOB` isn't large enough to store such a big file. 6.8 MiB https://dev.mysql.com/doc/refman/5.0/en/blob.html – Funk Forty Niner Sep 14 '15 at 14:47
  • Where is the code that is writing the content of the file to the response? You posted the code that is writing the header only. If that's your entire code, then that's the problem. After the header, you need to write the content of the file as a byte array to the response. – Racil Hilan Sep 14 '15 at 14:48
  • I updated the question. Please see it. Really need help. and also thanks for the comments. – Makudex Sep 14 '15 at 14:59
  • Injection vulnerability: check. Use of _deprecated_ extension: check. Please stop using `mysql`, it's deprecated. Switch to `PDO` or `mysqli` instead, and learn to use prepared statements – Elias Van Ootegem Sep 14 '15 at 14:59
  • 1
    Quoted from an answer in http://stackoverflow.com/questions/5775571/what-is-the-maximum-length-of-data-i-can-put-in-a-blob-column-in-mysql *"A BLOB can be 65535 bytes maximum. If you need more consider using a MEDIUMBLOB for 16777215 bytes or a LONGBLOB for 4294967295 bytes. See Storage Requirements for String Types for more info."* – Funk Forty Niner Sep 14 '15 at 15:19
  • Did you not see what I said [here...](http://stackoverflow.com/questions/32567649/download-full-size-of-file-in-php-mysql#comment52990508_32567649)? I didn't write *"BLOB **is probably not** large enough to store such a big file."* - I wrote *"BLOB **isn't** large enough to store such a big file."* "Isn't" as in **"is NOT".** – Funk Forty Niner Sep 14 '15 at 15:23
  • @Fred-ii- I got no problem storing blob and actually I used **MEDIUMBLOB** to store upto 16MB of file and what I actually did in my code was to trap that if the file is larger than **10MB** then I wouldn't be store in the db. **MEDIUMBLOB** not **BLOB**. If you could just see on my db table I am able to store the file. – Makudex Sep 14 '15 at 16:40
  • fair enough. well.. see Riggs' answer below. @Makudex – Funk Forty Niner Sep 14 '15 at 16:44
  • @Fred-ii- Yeah that's what I actually did. *Size checking* is the word :) – Makudex Sep 14 '15 at 16:59

1 Answers1

0

It seems to me you want to be echoing the file contents and not its filename

    $querysel = "Select * from file where file_id = '$fileid'";
    $resultsel = mysql_query($querysel);
    $rowsel = mysql_fetch_array($resultsel);

    $filename = $rowsel['file_name'];
    $mimetype = $rowsel['mime_type'];
    $filesize = $rowsel['file_size'];

    header("Content-length: ".$filesize);
    header("Content-type: ".$mimetype);
    header("Content-disposition: attachment; filename=".$filename);
    header("Content-Description: PHP Generated Data");
    header("Content-transfer-encoding: binary");

    echo $rowsel['file_content'];          //<-- changed line

Of course you still probably have a problem with the larger files in that a BLOB is not large enough to hold 6.8 Meg. See @Fred-ii- comment for details and links to all the relevant info you need to check that.

Even a LONGBLOB is only 4,294,967,295 bytes, thats a little over 4Gig, so you probably need to do some size checking before storing to even a LONGBLOB.

The safest suggestion would be to store the actual files on disk and only the filename and path in the database.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149