코드가 실행될 때 파일이 5MB를 초과하는지 검사하기 때문에 30 초의 시간 초과 오류가 발생합니다. 이 코드는 5MB 이상의 파일을 거부하도록 설계되어 있지만 파일이 5MB 미만인 경우 실행을 중지해야합니다. 파일 전송 덩어리가 비 었는지 확인하는 방법이 있습니까? 나는 현재 DaveRandom하여이 예를 사용하고 있습니다 :PHP는 완전히 다운로드 한 후 원격 파일 읽기를 중지합니다.
PHP Stop Remote File Download if it Exceeds 5mb
코드 DaveRandom 기준 :
$url = 'http://www.spacetelescope.org/static/archives/images/large/heic0601a.jpg';
$file = '../temp/test.jpg';
$limit = 5 * 1024 * 1024; // 5MB
if (!$rfp = fopen($url, 'r')) {
// error, could not open remote file
}
if (!$lfp = fopen($file, 'w')) {
// error, could not open local file
}
// Check the content-length for exceeding the limit
foreach ($http_response_header as $header) {
if (preg_match('/^\s*content-length\s*:\s*(\d+)\s*$/', $header, $matches)) {
if ($matches[1] > $limit) {
// error, file too large
}
}
}
$downloaded = 0;
while ($downloaded < $limit) {
$chunk = fread($rfp, 8192);
fwrite($lfp, $chunk);
$downloaded += strlen($chunk);
}
if ($downloaded > $limit) {
// error, file too large
unlink($file); // delete local data
} else {
// success
}
쇼 우리가 지금까지 가지고있는 코드를 사용하려고 노력하고있는 코드. – davidethell
메신저 DaveRandom의 답변에서 정확히 동일한 코드를 사용합니다. 그것은 첫 번째 답변 : http://stackoverflow.com/questions/13963158/php-stop-remote-file-download-if-it-exceeds-5mb – webdev
나는 그것을 이해하고 있지만, 관련있는 내용을 포함시키는 것이 좋습니다. 이 질문에 대한 독자의 이해가 가능하도록이 게시물의 코드 부분을 참조하십시오. – davidethell