如何使用PHP创建存储在MYSQL数据库中的文件的文件下载链接?

问题描述:

I know I can just upload the file to a folder in my server, save the directory location to the MYSQL database, and create a download link based on that directory location but what happens when you store a file like a pdf or zip archive directly on the MYSQL database? How do you retrieve that data and create a file download link then?

我知道我可以将文件上传到服务器中的文件夹,将目录位置保存到MYSQL数据库, 并根据该目录位置创建下载链接,但是当您将文件如pdf或zip存档直接存储在MYSQL数据库中时会发生什么? 那么如何检索该数据并创建文件下载链接呢? p> div>

You will have to set some content headers before outputting the data from your database, like:

header("Content-type: $type");
header("Content-length: $size");
header("Content-Disposition: attachment; filename=$name");
header("Content-Description: PHP Generated Data");
echo $data;

Full example at http://onlamp.com/pub/a/php/2000/09/15/php_mysql.html?page=3

You can create a php file like "download.php" that put the headers of that file type (and size) and the echo the contents of the file stored.

You shouldn't store file data in your database, that's what the file system is for!

However to answer your question you would store the file contents in a BLOB field which contains the binary data, then to download throw the correct headers and echo the field value.