tp3.2下载文件

tp3.2下载文件,下载excel,注意文件编码,直接上代码:

        header("Content-type:text/html;charset=utf-8");
        $file_name = "123.xlsx";//文件名,尽量不要用中文,如果用中文,需要转码
        $file_sub_path = DOC_ROOT . '/Download/temp/';//文件存放的路径

        $file_path=$file_sub_path.$file_name;
        if(file_exists($file_path) != true){
            dump('文件不存在');
			die();
        }

        $fp=fopen($file_path,"r");
        $file_size=filesize($file_path);
        //下载文件需要用到的头
        Header("Content-type: application/octet-stream");
        Header("Accept-Ranges: bytes");
        Header("Accept-Length:".$file_size);
        Header("Content-Disposition: attachment; filename=".$file_name);
        $buffer=1024;
        $file_count=0;

        while(!feof($fp) && $file_count<$file_size){
            $file_con=fread($fp,$buffer);
            $file_count+=$buffer;
            echo $file_con;
        }

        fclose($fp);

 

你可能感兴趣的:(php,php,下载,thinkphp)