简体   繁体   中英

how to create file download link to a file stored in MYSQL database with PHP?

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?

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.

您可以创建一个类似“ download.php”的php文件,该文件将放置该文件类型(和大小)的标头,并回显所存储文件的内容。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM