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

Posted by Cody Robertson on Stack Overflow See other posts from Stack Overflow or by Cody Robertson
Published on 2010-05-16T17:13:33Z Indexed on 2010/05/16 17:20 UTC
Read the original article Hit count: 206

Filed under:
|
|
|

Hi, 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();
        }
    }
}

© Stack Overflow or respective owner

Related posts about php

Related posts about headers