php 下载的文件损坏,PHP readfile()导致文件下载损坏

我使用

PHP脚本提供从我的网站下载必要的javascript计时器这个PHP脚本包含导致下载.但无论我尝试什么,下载的文件都会损坏.任何人都可以帮我指出我哪里出错了.

这是我的代码

include "db.PHP";

$id = htmlspecialchars($_GET['id']);

$error = false;

$conn = MysqL_connect(DB_HOST,DB_USER,DB_PASSWORD);

if(!($conn)) echo "Failed To Connect To The Database!";

else{

if(MysqL_select_db(DB_NAME,$conn)){

$qry = "SELECT Link FROM downloads WHERE ID=$id";

try{

$result = MysqL_query($qry);

if(MysqL_num_rows($result)==1){

while($rows = MysqL_fetch_array($result)){

$f=$rows['Link'];

}

//pathinfo returns an array of information

$path = pathinfo($f);

//basename say the filename+extension

$n = $path['basename'];

//NOW comes the action,this statement would say that WHATEVER output given by the script is given in form of an octet-stream,or else to make it easy an application or downloadable

header('Content-type: application/octet-stream');

header('Content-Length: ' . filesize($f));

//This would be the one to rename the file

header('Content-Disposition: attachment; filename='.$n.'');

//Finally it reads the file and prepare the output

readfile($f);

exit();

}else $error = true;

}catch(Exception $e){

$error = true;

}

if($error)

{

header("Status: 404 Not Found");

}

}

}

?>

  • 0
    点赞
  • 0
    收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:深蓝海洋 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值