>  기사  >  백엔드 개발  >  PHP IE에서 다운로드한 파일 이름이 깨졌을 경우 대처 방법

PHP IE에서 다운로드한 파일 이름이 깨졌을 경우 대처 방법

藏色散人
藏色散人원래의
2020-10-23 09:10:162043검색

PHP IE에서 다운로드한 잘못된 파일 이름에 대한 해결 방법: 1. 헤더 방법을 통해 잘못된 문자를 해결합니다. 2. "function remote_filesize($uri,$user='',$pw='') {.. .}" 왜곡된 코드를 해결하세요.

PHP IE에서 다운로드한 파일 이름이 깨졌을 경우 대처 방법

추천: "PHP 비디오 튜토리얼"

php 파일 다운로드 IE 파일 이름이 왜곡되는 문제

Chrome 브라우저를 사용해 왔지만 아무런 문제도 발견되지 않았습니다. 오늘 IE6를 사용하다가 파일을 다운로드할 때 파일명이 깨져서 IE에서 Thunder를 통해 다운로드할 때도 파일명이 깨져있는 것을 발견했습니다. 온라인으로 확인해보니 IE에서 인코딩하려면 urlencode를 사용해야 한다고 하더군요.

header('Content-Disposition: attachment; filename='.rawurlencode($file_name);)를 시도했는데 다운로드할 때 결과가 여전히 깨졌습니다. 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의 파일 크기 기능은 로컬 파일에만 사용할 수 있습니다. 원격 파일 처리가 실패하고 경고가 표시되며 Windows 플랫폼에 전달된 매개변수는 UTF-8을 사용해야 합니다.

인터넷에서 원격 파일을 얻는 네 가지 방법을 찾았습니다. 파일 크기 방법, 공유해 주셔서 감사합니다:

방법 1: 헤더

<?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

방법 2: 컬

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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.