php ie下載檔案名稱亂碼的解決方法:1、透過header方法解決亂碼;2、透過「function remote_filesize($uri,$user='',$pw='') {... }”等方法解決亂碼。
推薦:《PHP影片教學》
php檔下載IE檔名亂碼問題
一直都用chrome瀏覽器,沒發現問題。今天用ie6,發現檔案下載時檔名亂碼,ie下迅雷下載檔名也是亂碼。上網查了一下說在ie下需要使用urlencode編碼一下,我試了一下
header('Content-Disposition: attachment; filename='. rawurlencode($file_name);結果用ie下載還是亂碼。 php檔本身是gbk/gb2312編碼,於是我先將$file_name轉換成utf-8編碼再進行urlencode
header('Content-Disposition: attachment; filename='. rawurlencode(iconv("GBK" ,"UTF-8",$file_name)));這樣使用ie下載就沒問題了,難道urlencode只能對utf-8進行轉義編碼?
還有就是取得遠端檔案的大小問題,php中的filesize函數只能對本地文件進行處理,處理遠端文件會失敗並發出警告,並且在windows平台傳入的參數必須是gbk/gb2312編碼,使用utf-8編碼將無法存取系統中的資源。
在網路上找了四種取得遠端檔案大小的方法,多謝前輩們的分享,記錄一下:
方法一:header
<?php get_headers($url,true); //返回结果 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 ) ?>
這裡可以根據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;
以上是php ie下載檔名亂碼怎麼辦的詳細內容。更多資訊請關注PHP中文網其他相關文章!