user666923
user666923

Reputation: 87

How to download the file uploaded to a BLOB field using PhpMyAdmin?

I'm running a localhost mysql server with PhpMyAdmin version 4.2. I created a table with a MEDIUMBLOB column to store file. I can upload *.doc files correctly, but when I click the file link, it downloads it as a "tablename-columnname.bin" bin file.

Then I tried to manually rename this "tablename-columnname.bin" to "original_file.doc" and the doc can be opened correctly.

Question: Why phpmyadmin does not download the file as is? What can I do to fix it? I know a bit of php

Thanks!

Upvotes: 0

Views: 3150

Answers (2)

user666923
user666923

Reputation: 87

To make it a bit dynamic (based on MIME) I did the following change:

In tbl_get_field.php file, last few lines:


    $mime = PMA_detectMIME($result);
    switch($mime){
        case 'image/jpeg':
            $extension = 'jpg';    
        case 'image/gif':
            $extension = 'gif';
        case 'image/png':
            $extension = 'png';
        case 'application/pdf':
            $extension = 'pdf';
        default:
            $extension = 'bin';
    }
    PMA_downloadHeader(
        $table . '-' .  $_GET['transform_key'] . '.' . $extension,
        $mime,
        strlen($result)
    );
    echo $result;

Code above should output the file with correct extension instead of always ".bin".

In mime.lib.php file, we need to detect more MIME cases:


    function PMA_detectMIME(&$test)
    {
        $len = strlen($test);
        if ($len >= 2 && $test[0] == chr(0xff) && $test[1] == chr(0xd8)) {
            return 'image/jpeg';
        }
        if ($len >= 3 && substr($test, 0, 3) == 'GIF') {
            return 'image/gif';
        }
        if ($len >= 4 && substr($test, 0, 4) == "\x89PNG") {
            return 'image/png';
        }
        return 'application/octet-stream';
    }

I want to add pdf and word doc in this, but I dont know how to determine their MIME type base on the stream characters

Upvotes: 0

Prakashvgr
Prakashvgr

Reputation: 21

Use the 'header' before echo the file

header('Content-type: application/msword');

for dynamic way to find the 'Content type' use to store the 'file name' with 'extension' in database.

Upvotes: 1

Related Questions