开发者

Problem naming the download file

开发者 https://www.devze.com 2023-02-18 08:55 出处:网络
I have some php code which is suppose to download a zip file...this code is called from a file called download_file.php (very original, i know)

I have some php code which is suppose to download a zip file...this code is called from a file called download_file.php (very original, i know)

The relevant function is as follows :

function download_clientfile() {
    if ($this->download_file === FALSE) {
        $this->log->log(sprintf("The download file was not generated."), PEAR_LOG_CRIT);
        return;
    }

    $fsize = filesize($this->download_file);
 开发者_C百科   $path_parts = pathinfo($this->download_file);
    $ext = strtolower($path_parts["extension"]);

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; file='.$path_parts['basename']);
    header('Content-Transfer-Encoding: binary');
    header('Expires:0');
    header("Cache-control: must-revalidate, post-check=0, pre-check=0");
    header('Pragma: public');
    header('Content-Length: '.filesize($this->download_file));
    ob_clean();
    flush();
    readfile($this->download_file);
    exit;

}


Problem : The file downloaded is the zip file that is expected, however its downloaded with the name 'download_file.php'

Any suggestions on how I can get it to download by the name of the zip file. Thanks


Use filename instead of file:

header('Content-Disposition: attachment; filename='.$path_parts['basename']);

Also, you may encounter browsers which use the extension anyway. In that case, you can call the script like this:

download_file.php?file=myfile.zip

or:

download_file.php/myfile.zip

This way, the URL ends in .zip, which tricks the browser into thinking it is downloading a ZIP file.


Try changing file= to filename=

header('Content-Disposition: attachment; filename='.$path_parts['basename']);


use

header("Content-Disposition: attachment; filename=\"$filename\"");
0

精彩评论

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