为什么我的下载文件被损坏或损坏? [英] why my downloaded file is alwayes damaged or corrupted?

查看:112
本文介绍了为什么我的下载文件被损坏或损坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i have a very weird problem with my download script

它基本上


1.使用GET方法编写文件ID

1.gets a file id with "GET" method

2.从数据库中读取该文件的名称和位置

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

3.将它发送给具有头文件和readfile的客户端

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

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

but strangely that file always comes out as corrupted or damaged

喜欢它是一个zip或rar文件
文件大小是正确的,它打开OK

like if it's a zip or rar file file size is right and it opens ok

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

but i cant open compressed files inside of it and that's when i get the file damaged error

这是奇怪的cuz如果代码有一个问题,我不应该甚至能够打开zip文件(或至少我想我不应该)

which is weird cuz if the code had a problem i shouldn't even be able to open the zip file(or at least i think i shouldn't)

另一件事情是我在发送头文件之前打印出文件的路径只是为了确保一切正常

another thing is i've printed out the file with it's path right before sending the headers just to be sure everything is ok

我把文件地址放在url上,下载文件,文件没有错误就是没有错误

i've put the file address on the url and download the file , file was ok with no errors

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

so everything is fine before sending the headers

这里是我的代码

        $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 '<br />';
        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;

我已经检查了标题几次,他们很好(我想),我也添加人们知道的每个标题只是为了确定!

i've checked the headers couple times and they are fine(i think) , i've also add every headers known to man just to be sure !

我开始想到,除了脚本
,像char编码或文件夹权限一样!或者类似的东西!!

i'm starting to think maybe it's something other than script like char encoding or folder permission ! or something like that !!

我是否缺少某些东西?

推荐答案

似乎分配的代码只是强制下载,这里是一个很好的功能,我一直使用。它也将处理超过2GB的文件。

That seems allot of code just to force-download, here is a nice function I use all the time. It will handle files over 2GB too.

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

/*finding file info*/
$file = comon::get_all_by_condition('files', 'id', $file_id);
$path = $file['location'] . '/' . $file['file_name'];

if (!is_file($path)) {
    echo 'File not found.('.$path.')';
} elseif (is_dir($path)) {
    echo 'Cannot download folder.';
} else {
    send_download($path);
}

return;

//The function with example headers
function send_download($file) {
    $basename = basename($file);
    $length   = sprintf("%u", filesize($file));

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="' . $basename . '"');
    header('Content-Transfer-Encoding: binary');
    header('Connection: Keep-Alive');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . $length);

    set_time_limit(0);
    readfile($file);
}
?>

这篇关于为什么我的下载文件被损坏或损坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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