如何在PHP中进行下载链接? [英] how to make a download link in PHP?

查看:162
本文介绍了如何在PHP中进行下载链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用sql在数据库中上传文件,但如何为其下载链接?喜欢当你在线下载东西时,会出现一个消息框,你会被问到是否要用程序打开它或保存它。我怎么能在php中这样做?你可以给我代码吗?我还是一个noob。

i can upload a file in the database using sql but how can i make a download link for it? like when you download something online then a message box will come up you will be asked if you would like to open it with a program or save it. how can i do that in php? can you give me the codes for it? i'm still a noob.

推荐答案

将此代码放在页面上(连同PHP代码一起从DB获取信息并将其放在变量中对于名称/大小/数据,然后链接到该页面。

Place this code on a page (along with the PHP code to get the info from the DB and place it in the variables for the name/size/data, then link to that page.

<?php
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $name_of_file);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size_of_file);

echo $file_data;
?>

上面列出的所有标题都不是必需的 - 实际上只有Content-Type头是真正的 需要使下载工作正常。Content-Disposition头是很好的包括,以便您可以指定一个正确的文件名;其他只是帮助浏览器更好地处理下载,如果你愿意,可以省略。

Not all of the headers listed above are strictly necessary - in fact, only the Content-Type header is truly necessary to make the download work properly. The Content-Disposition header is good to include so that you can specify a proper filename; the others just help the browser handled the download better and can be omitted if you desire.

这篇关于如何在PHP中进行下载链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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