如何从PHP数据库中下载基于blob的文件? [英] how to download blob based file from MySQL database in PHP?

查看:371
本文介绍了如何从PHP数据库中下载基于blob的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使PHP脚本允许我从MySQL中的数据库下载文件。
我有一个名为的文件,其中上传的文件保存在基于 BLOB 的字段中。

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.

我尝试过这个查询,其中参数通过(fileID,fileName,fileType)到download.php页面:

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.

如何将file1作为JPEG文件下载到我的硬盘?

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

推荐答案

这是在处理blob文件时遇到的最常见的问题,从您的示例中,我可以看到您正在将fileType保存为文件的扩展名(即图像的JPG,pdf文件的pdf等),您正在上传。但是,您可以将文件类型保存为Mime内容类型。假设如果您上传jpeg图像,MIME类型将作为image / jpeg存储在fileType中。同样的,pdf将被存储为application / pdf。我设计了这样的代码来从数据库下载blob文件,这里我假设文件已经上传到您创建的数据库表中

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. For 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 blob file from the database,here i assumed that the files are already uploaded into the database table you created

数据库表上传

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

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

download.php

  <?php
             $connection =  mysqli_connect("localhost","root"," ",your_database)
                             or die('Database Connection Failed');
             mysqli_set_charset($connection,'utf-8');

          $id = 1;
          $query = "SELECT * " ."FROM uploads WHERE userID = '$id'";
          $result = mysqli_query($connection,$query) 
                     or die('Error, query failed');
         list($id, $file, $type, $size,$content) =   mysqli_fetch_array($result);
           //echo $id . $file . $type . $size;
         header("Content-length: $size");
         header("Content-type: $type");
         header("Content-Disposition: attachment; filename=$file");
         ob_clean();
         flush();
         echo $content;
         mysqli_close($connection);
         exit;

        ?>

请在这里找到blob-upload和download的完整代码

please find the complete code of blob-upload and download here Blob-upload-download

这篇关于如何从PHP数据库中下载基于blob的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆