如何从 PHP 中的 MySQL 数据库下载基于 blob 的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21291361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:00:09  来源:igfitidea点击:

how to download blob based file from MySQL database in PHP?

phpmysqldownloadblob

提问by user1386999

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 fileswhere the uploaded file is saved in a BLOBbased field.

如何制作允许我从 MySQL 数据库下载文件的 PHP 脚本。我有下表命名files上传的文件保存在BLOB基于字段的位置。

+-------+----------+----------+------------+-------------------+--------+
|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:

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

$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?

例如,我如何将 file1 作为 JPEG 文件下载到我的硬盘驱动器?

回答by Sampath

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.

这是处理 blob 文件时最常见的问题。从您的示例中,我可以看到您将“fileType”保存为文件的扩展名(即图像为“jpg”,pdf 文件为“pdf”等),您正在上传。但是,您可以将文件类型保存为 MIME 内容类型,而不是这样。

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.

假设您上传 jpeg 图像 - MIME 类型将作为“图像/jpeg”存储在“fileType”中。同样,对于 pdf,它将存储为“application/pdf”。我设计了这样的代码来从数据库下载 blob 文件。我将假设文件已经上传到您创建的数据库表中。

Database table "uploads"

数据库表“上传”

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

| 文件ID | 文件名 | 文件类型 | 文件大小 |文件数据 | 用户 ID |

download.php

下载.php

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

$id = 1;

// Use a prepared statement in production to avoid SQL injection;
// we can get away with this here because we're the only ones who
// are going to use this script.
$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);
header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$file");
ob_clean();
flush();
echo $content;
mysqli_close($connection);
exit;

?>

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

您可以在此处找到 blob-upload 的完整代码。

回答by meisamphp

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

并在 download.php 中使用此代码

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