开发者

Force download working, but showing invalid when trying to open locally

I wrote this function and everything works well till i try to open the downloaded copy and it shows that the file is invalid. Here is my function


    function download_file() {
    //Check for download request:
    if(isset($_GET['file'])) {
        //Make sure there is a file before doing anything
        if(is_file($this->path . basename($_GET['file']))) {
            //Below required for IE:
            if(ini_get('zlib.output_compression')) {
                ini_set('zlib.output_compression', 'Off');
            }

            //Set Headers:
            header('Pragma: public');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $this->path . basename($_GET['file'])) . ' GMT');
            header('Content-Type: application/force-download');
            header('Content-Disposition: inline; filename="' . basename($_GET['file']) . '"');
            header('Content-Transfer-Encoding: binary');
            header('Content-Length: ' . filesize($this->path . basename($_GET['file'])));
            header('Connection: close');
            readfile($this->path . basename($_GET['file']));
            exit();
        }
    }
}

EDIT: By invalid for example I download a picture and try to view it in iPhotos or Windows Picture View开发者_StackOverflower and it says, File Format Unsupported. When I view it on the server it looks fine but after download it is corrupt.


Thanks Gumbo, tried that and it outputted:

Warning: gmdate() expects parameter 2 to be long, string given in C:\Program Files\Wamp Server\www\TutToasterUpload\PHPClass.php on line 83
lets see what happens

Fixed this line:

//Added filemtime();    
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', filemtime($this->path . basename($_GET['file']))) . ' GMT');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜