关闭
doumiebiao6827 2013-09-05 19:57
浏览 47
已采纳

文件在浏览器上显示而不是在YII网站上下载

The file getting displayed in browser instead getting downloaded. It was working before. I made some changes to other part of my project.but I didn't touch this function, I have no idea what happened . please help me.

         $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;
  • 写回答

2条回答 默认 最新

  • dongyi3776 2013-09-05 21:36
    关注

    I'm not entirely sure what would cause this to stop functioning, unless perhaps you changed PHP versions, however try the following:

    Set the Content-Length header like this:

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

    And you can utilise readfile to avoid memory issues like this, it will read a file and output it directly to the output stream.

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

    Another possibility is that somewhere else in the application you are outputting data, prior to setting these headers - this will cause issues as headers can only be set prior to any output.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部