【问题标题】:File getting displayed on browser instead downloading in YII website文件显示在浏览器上,而不是在 YII 网站上下载
【发布时间】:2013-09-06 03:57:19
【问题描述】:

文件显示在浏览器中,而不是下载。它以前工作过。我对项目的其他部分进行了一些更改。但我没有触及这个功能,我不知道发生了什么。请帮帮我。

         $filename=date("d-m-ys").".sql";
        $handle = fopen($filepath."/".date("d-m-ys").".sql", 'w+');
        fwrite($handle, $return);
        fclose($handle);

        $bits = @file_get_contents($filepath."/".date("d-m-ys").".sql");
        header("Content-type: application/sql");
        header('Content-Disposition: attachment; filename="'.$filename.'"');
        print $bits;

【问题讨论】:

    标签: php yii http-headers download mysqldump


    【解决方案1】:

    我不完全确定什么会导致它停止运行,除非您更改了 PHP 版本,但请尝试以下操作:

    像这样设置 Content-Length 标头:

    header("Content-Length: ".filesize($filepath."/".date("d-m-ys").".sql"));
    

    你可以利用 readfile 来避免这样的内存问题,它会读取一个文件并将其直接输出到输出流。

      readfile($filepath."/".date("d-m-ys").".sql");
    

    另一种可能性是,在设置这些标头之前,您在应用程序的其他地方输出数据 - 这将导致问题,因为标头只能在任何输出之前设置。

    【讨论】:

      【解决方案2】:

      您可以尝试使用 htaccess 强制下载,

      <FilesMatch "\.(?i:sql)$">
       ForceType application/octet-stream
       Header set Content-Disposition attachment
      </FilesMatch>
      

      【讨论】:

        猜你喜欢
        • 2017-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-16
        • 1970-01-01
        • 2012-01-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多