文件在浏览器上显示而不是在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;

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.

You could try forcing the download using htaccess,

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