php文件下载代码怎么写?

会说话的狸猫 2010-08-05 05:28:10
download.php?id=1 点击之后下载文件

$id = isset($_REQUEST['id']) ? trim($_REQUEST['id']) : 0;
$file_path = 'http://www.********.com/file/';
$file_name = 'aaa_'.$id.'.rar';

if (file_exists($file_path . $file_name)){

header( "Pragma: public" );
header( "Expires: 0" );
header( 'Content-Encoding: none' );
header( "Cache-Control: must-revalidate, post-check=0, pre-check=0" );
header( "Cache-Control: public" );
header( "Content-type: application/octet-stream\n" );
header( "Content-Description: File Transfer" );
header( 'Content-Disposition: attachment; filename=' . $file_name );
header( "Content-Transfer-Encoding: binary" );
header( 'Content-Length: ' . filesize ( $file_path . $file_name ) );
readfile ( $file_path . $file_name );

} else {
echo '<script>alert("获取下载地址出错!")</script>';
echo '<script>location.replace("download.php");</script>';
exit;
}


这样可以使用浏览器另存为的方式下载,但如果是迅雷下载时显示下载文件为download.php这是怎么回事?如果是不弹出迅雷的下载窗口直接使用浏览器自带的下载方式改怎么写呢?有没有什么方法把文件存放的地址直接导向到迅雷的url下载地址?还有其它的下载文件的代码写法吗?
...全文
901 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dleno 2010-08-06
  • 打赏
  • 举报
回复
if (file_exists($file_path . $file_name)){
echo 'location.replace("'.$file_path . $file_name.'");';
exit;
} else {
echo 'alert("获取下载地址出错!");';
echo 'location.replace("download.php");';
exit;
}

下载时用ajax请求,并在js里eval返回结果
ytlines 2010-08-06
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 happy664618843 的回复:]

file_exists($filepath.$filename){
header("Content-length:".filesize($filename));
header( 'Content-Disposition: attachment; filename=' . $file_name );

}
[/Quote]


happy664618843 2010-08-06
  • 打赏
  • 举报
回复
file_exists($filepath.$filename){
header("Content-length:".filesize($filename));
header( 'Content-Disposition: attachment; filename=' . $file_name );

}
会说话的狸猫 2010-08-05
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 dingsongtao 的回复:]

最直接的
PHP code

if (file_exists($file_path . $file_name)){
echo '<script>location.replace("'.$file_path . $file_name.'");</script>';
exit;
} else {
echo '<script>alert("获取下载地址出错!")</scr……
[/Quote]

这位兄弟这样倒是可以但是当前页面变成空白页了,怎么使页面保持不变呢?
Dleno 2010-08-05
  • 打赏
  • 举报
回复
最直接的

if (file_exists($file_path . $file_name)){
echo '<script>location.replace("'.$file_path . $file_name.'");</script>';
exit;
} else {
echo '<script>alert("获取下载地址出错!")</script>';
echo '<script>location.replace("download.php");</script>';
exit;
}
CunningBoy 2010-08-05
  • 打赏
  • 举报
回复
checkLogin中可以使用xmlhttp提交给php处理
会说话的狸猫 2010-08-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 cunningboy 的回复:]

<a href="http://www.******.com/file/aaa_1.rar" onClick="checkLogin();">
在onClick中检查登录。
[/Quote]

请问怎么使用js检测用户登录状态,程序里面是使用session,js里怎么判断?
CunningBoy 2010-08-05
  • 打赏
  • 举报
回复
<a href="http://www.******.com/file/aaa_1.rar" onClick="checkLogin();">
在onClick中检查登录。
会说话的狸猫 2010-08-05
  • 打赏
  • 举报
回复
我知道可以用这种方式直接下载
<a href="http://www.******.com/file/aaa_1.rar">下载文件</a>


但我的想法当是用户点击这个下载链接时,如果未登录跳转到登录页面,否则直接弹出下载窗口。所以我通过传递参数的方式到Php文件,先判断用户的登录状态再根据参数返回文件的地址。

这样的话如果是迅雷下载就不能获取原始的文件存放地址了,出现了下载download.php的情况。
mr_fang 2010-08-05
  • 打赏
  • 举报
回复
需要这么复杂么~?

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧