php ie下載檔名亂碼怎麼辦

藏色散人
發布: 2023-03-06 19:20:01
原創
2048 人瀏覽過

php ie下載檔案名稱亂碼的解決方法:1、透過header方法解決亂碼;2、透過「function remote_filesize($uri,$user='',$pw='') {... }”等方法解決亂碼。

php ie下載檔名亂碼怎麼辦

推薦:《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=&#39;&#39;,$pw=&#39;&#39;) { // 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(&#39;Authorization: Basic &#39; . base64_encode($user.&#39;:&#39;.$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 &#39;<br>head-->&#39;.$head.&#39;<----end <br>&#39;; // gets you the numeric value from the Content-Length // field in the http header $regex = &#39;/Content-Length:\s([0-9].+?)\s/&#39;; $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 = &#39;unknown&#39;; } //$last=round($size/(1024*1024),3); //return $last.&#39; MB&#39;; return $size; } 方法三:fsock
function getFileSize($url) { $url = parse_url($url); if($fp = @fsockopen($url[&#39;host&#39;],emptyempty($url[&#39;port&#39;])?80:$url[&#39;port&#39;],$error)) { fputs($fp,"GET ".(emptyempty($url[&#39;path&#39;])?&#39;/&#39;:$url[&#39;path&#39;])." HTTP/1.1\r\n"); fputs($fp,"Host:$url[host]\r\n\r\n"); while(!feof($fp)) { $tmp = fgets($fp); if(trim($tmp) == &#39;&#39;) { break; } elseif(preg_match(&#39;/Content-Length:(.*)/si&#39;,$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中文網其他相關文章!

相關標籤:
php
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!