鍍金池/ 問答/PHP/ thinkphp文件下載,下載下的文件損壞

thinkphp文件下載,下載下的文件損壞

public function download(){
        $filepath='./uploads/1ce97664a4523e99cc623ad39d98c5b8.jpg';
        if(!file_exists($filepath)){
            $filepath=iconv('utf-8','gb2312',$filepath);
            if(!file_exists($filepath)){
                exit('文件路徑錯誤');
            }
        }
        $file_size=filesize($filepath);
        header('content-type:application/octet-stream');
        header('accept-ranges:bytes');
        header('accept-length:'.filesize($filepath));
        header('content-disposition:attachment;filename=1ce97664a4523e99cc623ad39d98c5b8.jpg');
        $handle=fopen($filepath,'r');
        $buffer=1024;
        $tot=0;
        while(!feof($handle)&&$tot<$file_size){
            $file_content=fread($handle,$buffer);
            $tot+=$buffer;
            echo $file_content;
        }
        fclose($handle);
    }

這段代碼,我要不放在thinkphp里用,下載下來的文件可以打開。
但放在thinkphp里用,下載下來的文件就打不開,什么損壞之類的。
求大神解答

回答
編輯回答
生性

好像解決了,加了
`ob_clean();
flush();`,就好了,什么清除緩沖區(qū),刷新緩沖區(qū)。不太懂

2018年5月29日 16:31