Curl을 사용하여 GoogleCode에서 원격 파일의 파일 크기를 얻는 방법은 무엇입니까?
문제
CURL을 실제로 다운로드하지 않고 CURL을 사용하여 원격 파일 "Compiler-Latest.zip"(GOOGLECODE.com)의 파일 크기를 얻으려고합니다. 여기 내 PHP 코드가 있습니다.
$url = 'http://closure-compiler.googlecode.com/files/compiler-latest.zip';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // optional
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); // optional
curl_setopt($ch, CURLOPT_TIMEOUT, 60); // optional
$result = curl_exec($ch);
$filesize = curl_getinfo($ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD);
curl_close($ch);
print 'Filesize: ' . $filesize . '<br><br>';
print_r($result);
그러나이 오류 404 문서의 파일 크기 (1379 바이트)가있는 "http/1.1 404 찾을 수 없음"상태를 얻습니다. 따라서 설정하면 (Curlopt_nobody, 0) 파일을 다운로드하고 올바른 파일 크기 (현재 3820320 바이트)를 반환합니다. 내 질문은 "Compiler-Latest.zip"파일의 올바른 파일 크기를 다운로드하지 않고 얻는 방법입니다.
중요 :이 코드는 GOOGLECODE.com 이외의 다른 URL에서 예상대로 작동합니다.
해결책
사용 get_headers 기능:
<?php
$headers = get_headers('http://closure-compiler.googlecode.com/files/compiler-latest.zip');
$content_length = -1;
foreach ($headers as $h)
{
preg_match('/Content-Length: (\d+)/', $h, $m);
if (isset($m[1]))
{
$content_length = (int)$m[1];
break;
}
}
echo $content_length;
다른 팁
컨텐츠 길이가 누락 될 때 파일 크기를 얻는 방법은 무엇입니까?
제휴하지 않습니다 StackOverflow