您好,登錄后才能下訂單哦!
小編給大家分享一下php中ie下載文件名亂碼的解決方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
php ie下載文件名亂碼的解決辦法:1、通過header方法解決亂碼;2、通過“function remote_filesize($uri,$user='',$pw='') {...}”等方法解決亂碼。
php文件下載IE文件名亂碼問題
一直用chrome瀏覽器,沒發(fā)現(xiàn)問題。今天用ie6,發(fā)現(xiàn)文件下載時(shí)文件名亂碼,ie下迅雷下載文件名也是亂碼。網(wǎng)上查了下說在ie下需要使用urlencode編碼一下,我試了下
header('Content-Disposition: attachment; filename='. rawurlencode($file_name);結(jié)果用ie下載還是亂碼。php文件本身是gbk/gb2312編碼,于是我先將$file_name轉(zhuǎn)換成utf-8編碼再進(jìn)行urlencode
header('Content-Disposition: attachment; filename='. rawurlencode(iconv("GBK","UTF-8",$file_name)));這樣使用ie下載就沒問題了,難道urlencode只能對(duì)utf-8進(jìn)行轉(zhuǎn)義編碼?
還有就是獲取遠(yuǎn)程文件的大小問題,php中的filesize函數(shù)只能對(duì)本地文件進(jìn)行處理,處理遠(yuǎn)程文件會(huì)失敗并發(fā)出一條警告,并且在windows平臺(tái)傳入的參數(shù)必須是gbk/gb2312編碼,使用utf-8編碼將無法訪問系統(tǒng)中的資源。
在網(wǎng)上找了四種獲取遠(yuǎn)程文件大小的方法,多謝前輩們的分享,記錄一下:
方法一:header
<?php get_headers($url,true); //返回結(jié)果 Array ( [0] => HTTP/1.1 200 OK [Date] => Sat, 29 May 2004 12:28:14 GMT [Server] => Apache/1.3.27 (Unix) (Red-Hat/Linux) [Last-Modified] => Wed, 08 Jan 2003 23:11:55 GMT [ETag] => "3f80f-1b6-3e1cb03b" [Accept-Ranges] => bytes [Content-Length] => 438 [Connection] => close [Content-Type] => text/html ) ?>
這里可以根據(jù)Content-Length直接獲取大小了。
方法二:curl
function remote_filesize($uri,$user='',$pw='') { // start output buffering ob_start(); // initialize curl with given uri $ch = curl_init($uri); // make sure we get the header curl_setopt($ch, CURLOPT_HEADER, 1); // make it a http HEAD request curl_setopt($ch, CURLOPT_NOBODY, 1); // if auth is needed, do it here if (!emptyempty($user) && !emptyempty($pw)) { $headers = array('Authorization: Basic ' . base64_encode($user.':'.$pw)); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } $okay = curl_exec($ch); curl_close($ch); // get the output buffer $head = ob_get_contents(); // clean the output buffer and return to previous // buffer settings ob_end_clean(); echo '<br>head-->'.$head.'<----end <br>'; // gets you the numeric value from the Content-Length // field in the http header $regex = '/Content-Length:\s([0-9].+?)\s/'; $count = preg_match($regex, $head, $matches); // if there was a Content-Length field, its value // will now be in $matches[1] if (isset($matches[1])) { $size = $matches[1]; } else { $size = 'unknown'; } //$last=round($size/(1024*1024),3); //return $last.' MB'; return $size; } 方法三:fsock function getFileSize($url) { $url = parse_url($url); if($fp = @fsockopen($url['host'],emptyempty($url['port'])?80:$url['port'],$error)) { fputs($fp,"GET ".(emptyempty($url['path'])?'/':$url['path'])." HTTP/1.1\r\n"); fputs($fp,"Host:$url[host]\r\n\r\n"); while(!feof($fp)) { $tmp = fgets($fp); if(trim($tmp) == '') { break; } elseif(preg_match('/Content-Length:(.*)/si',$tmp,$arr)) { return trim($arr[1]); } } return null; } else { return null; } } 方法四:file_get_contents $fCont = file_get_contents("http://www.cnmiss.cn/"); echo strlen($fCont)/1024;
看完了這篇文章,相信你對(duì)php中ie下載文件名亂碼的解決方法有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。