3

How can i make a PHP script that will allow me to download a file from a database in MySQL. I have the following table named files where the uploaded file is saved in a BLOB based field.

+-------+----------+----------+------------+-------------------+--------+
|fileID | fileName | fileType | fileSize   |fileData           | userID |
+-------+----------+----------+------------+-------------------+--------+
| 1     | file1    | JPEG     | 211258     |[BLOB - 206.3 KiB] | 1      |
| 2     | file2    | PNG      | 211258     |[BLOB - 201.3 KiB] | 1      |
+-------+----------+----------+------------+-------------------+--------+

I cannot figure out a way to call the file to download and every time i try, i get the binary data of the file, such as random numbers and symbols.

I have tried this query where parameters are passed through (fileID, fileName, fileType) to a download.php page:

$id = mysqli_real_escape_string($link, $_GET['fileID']);
$name = mysqli_real_escape_string($link, $_GET['fileName']);
$type = mysqli_real_escape_string($link, $_GET['fileType']);

$SELECT = "SELECT * FROM files WHERE fileID = $id AND fileName = $name ";
$result = mysqli_query($SELECT, $link);
$result = mysqli_fetch_assoc($result);

header("Content-type: $type");  
echo $result['fileData'];

But this leads to a blank page with no output.

How can I download, for example, file1 as a JPEG file to my hard drive?

7
  • 1
    a good reason not to store files in the db. Commented Jan 22, 2014 at 18:54
  • @Dagon Where should i store them then? Commented Jan 22, 2014 at 18:56
  • 1
    Content-type should be image/jpeg that may or may not fix the issue you are having. Honestly you should have just stored paths of the images in the database instead of storing them as blobs Commented Jan 22, 2014 at 18:56
  • 1
    files should be stored in the files system Commented Jan 22, 2014 at 18:57
  • @elitechief21 the image example i used in the question above is just an example. i want to allow zip folders and word documents to be stored as well. Commented Jan 22, 2014 at 18:58

2 Answers 2

12

This is the most common problem faced while dealing with the blob file. From your example, I can see that you are saving "fileType" as the extensions of the files (i.e 'jpg' for images, 'pdf' for pdf files etc.), you are uploading. But instead of that you can can save file type as the MIME content type.

Suppose if you upload a jpeg image - the MIME type will be stored in the "fileType" as the "image/jpeg". Similarly for pdf it will be stored as "application/pdf". I designed code like this to download the blob file from the database. I am going to assume that the files are already uploaded into the database table you created.

Database table "uploads"

| fileID | fileName | fileType | fileSize |fileData | userID |

download.php

<?php
$connection =  mysqli_connect("localhost","root"," ",your_database);

$id = 1;

$query = "SELECT * FROM uploads WHERE userID = ?";
$result = $connection->execute_query($query, [$id]); 
list($id, $file, $type, $size, $content) = mysqli_fetch_array($result);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$file");
echo $content;

You can find the complete code of blob-upload here.

Sign up to request clarification or add additional context in comments.

2 Comments

this worked fine, when i directed it from HTML to PHP and it forced a download, but with jQuery & PHP it does not work.The content gets echoed in the console, but no file is downloaded!
@tmzafar yep, that's how AJAX works - the response goes back to the JavaScript. Don't expect to download files via Ajax.
-1

you can use session and post filename and file content to download page

session_start();
$_SESSION['filename'] = $filename
$_SESSION['file'] = $file
<a href="download.php">echo "Download File"</a>

and in download.php use this code

session_start();
$filename = $_SESSION['filename'];
$file = $_SESSION['file'];
header("Content-Disposition: attachment; filename=$filename");
ob_clean();
flush();
echo $file;

1 Comment

it has nothing to do with session variables but with mime type

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.