2011-03-21 78 views
0

我有的想下载一个zip文件一些PHP代码...此代码是从一个名为叫download_file.php(非常原始的,我知道)问题命名下载文件

相关功能如下:

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); 
    $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; 

}


问题:下载的文件是预期的zip文件,但其与T下载他名为'download_file.php'

关于如何通过压缩文件的名称下载它的任何建议。 感谢

回答

1

使用文件名代替文件

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

此外,您可能会遇到哪些反正使用扩展的浏览器。在这种情况下,你可以这样调用脚本:

download_file.php?file=myfile.zip 

或:

download_file.php/myfile.zip 

这样,URL在.ZIP,结束其招数浏览器以为它正在下载一个ZIP文件。

+0

谢谢。至少在Firefox和Chrome中,更改为**文件名**而不是**文件**取得了诀窍。 – sunil 2011-03-21 16:07:59

0

尝试更改文件=为filename =

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

0

使用

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