php 下载的文件损坏,php – 为什么我下载的文件总是被损坏或损坏?

我的下载脚本有一个非常奇怪的问题

它基本上

1.gets a file id with “GET” method

2.gets the name and location of that file from database

3.sends it to the client with the headers and readfile

但奇怪的是,该文件总是出现损坏或损坏

比如,如果它是zip或rar文件

文件大小是正确的,它打开确定

但我不能打开它内部的压缩文件,那是我得到文件损坏的错误

这是奇怪的因为如果代码有问题我甚至不能打开zip文件(或至少我认为我不应该)

另一件事是我在发送标题之前用它的路径打印出文件,以确保一切正常

我把文件地址放在网址上并下载文件,文件没问题

所以在发送标题之前一切都很好

这是我的代码

$file_id = isset($_GET['id']) && (int)$_GET['id'] != 0 ? (int)$_GET['id'] : exit;

finging file info

$file = comon::get_all_by_condition('files' , 'id' , $file_id );

if(!$file) exit;

foreach($file as $file){

$location = $file['location'];

$filename = $file['file_name'];

}

/

$site = comon::get_site_domian();

$ext = trim(end(explode('.' , $filename )));

$abslout_path = 'http://'.$site.'/'.$location.'/'.$filename;

$relative = $location.'/'.$filename;

// content type

switch($ext) {

case 'txt':

$cType = 'text/plain';

break;

case 'pdf':

$cType = 'application/pdf';

break;

case 'zip':

$cType = 'application/zip';

break;

case 'doc':

$cType = 'application/msword';

break;

case 'xls':

$cType = 'application/vnd.ms-excel';

break;

case 'ppt':

$cType = 'application/vnd.ms-powerpoint';

break;

case 'gif':

$cType = 'image/gif';

break;

case 'png':

$cType = 'image/png';

break;

case 'jpeg':

case 'jpg':

$cType = 'image/jpg';

break;

default:

$cType = 'application/force-download';

break;

}

just checking

if(!file_exists($relative)){

echo $relative;

echo '
';

exit;

}

if( !is_readable( $relative ) )

exit('its not redable');

if( headers_sent() )

exit('headers ? already sent !! ');

header( 'Pragma: public' );

header( 'Expires: 0' );

header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );

header( 'Cache-Control: private', false ); // required for certain browsers

header( 'Content-Description:File Transfer' );

header($_SERVER['SERVER_PROTOCOL'].' 200 OK');

header( 'Content-Type:'.$cType);

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

header( 'Content-Transfer-Encoding: binary' );

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

readfile($abslout_path);

exit;

我已经检查了几次标题并且它们很好(我认为),我还添加了人类已知的每个标题以确保!

我开始认为它可能不是脚本

像char编码或文件夹权限!或类似的东西 !!

我错过了什么吗?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值