php实现下载功能无乱码

对于一串乱码,身为中国人的我头大了。网上找一大坨代码,终于碰到狗屎运找了无码的下载功能,动了些小手术。稍微改动一下$file_name和$file_path就可以使用了。


       //下载 
    function downFile(){ $ua = $_SERVER["HTTP_USER_AGENT"]; $file_name = "filename.pdf"; $file_path = "./public/files/"; $full_path = $file_path .$file_name; if(is_file($full_path)){//判断该文件是否存在 $encoded_filename = urlencode($file_name); $encoded_filename = str_replace("+", "%20", $encoded_filename); header('Content-Type: application/octet-stream');//输出为文件流,告诉浏览器,要下载文件。 if(preg_match("/MSIE/", $ua)){//是ie浏览器,则使用下面方式 header('Content-Disposition: attachment; filename="' . $encoded_filename . '"'); }else if(preg_match("/Firefox/", $ua)) {//是火狐浏览器,则使用下面方式 header('Content-Disposition: attachment; filename*="utf8\'\'' . $file_name . '"'); }else{//其他浏览器则使用下面的方式 header('Content-Disposition: attachment; filename="' . $file_name . '"'); } readfile($full_path);//输出内容,下载。 }else{ echo "no file"; } }


posted on 2014-09-26 18:24  sustudy  阅读(96)  评论(0)    收藏  举报

导航