본문 바로가기
Tip & Tech/php

PHP- 파일 다운로드시 속도 제한하기

by 변기환 2011. 7. 29.
파일 전송은 네트워크에 많은 부하를 준다. 파일 용량이 수십, 수백 Mb 혹은 그 이상인 경우 그대로 내려받게 하지 말고 전송속도를 제한하는것이 좋다.

filesize 함수를 사용하여 지정된 사이즈보다 작으면 그대로 전송하고, 크다면 전송량을 제한 하는것도 좋은 방법이다.
<?
// 서버에 저장된 파일명
$local_file = 'Original_File.zip';

// 전송속도(다운로드 되는 속도) 제한 (=--> 50 kb/s)
$download_rate = 50;

// 반드시 다운로드할 파일이 있을 때에 실행되도록 한다.
if(file_exists($local_file) && is_file($local_file))
{
    // 헤더에 파일 관련 정보 전송
    header('Cache-control: private');
    header('Content-Type: application/octet-stream');
    header('Content-Length: '.filesize($local_file));
    header('Content-Disposition: filename='.$local_file);
 
    // flush 함수는 출력물을 브라우저로 보내고, 출력 버퍼를 비우는 역할을 한다.
    flush();
 
    // open file stream
    $file = fopen($local_file, "r");
 
    while (!feof($file))
    {
        // send the current file part to the browser
        print fread($file, round($download_rate * 1024));
 
        // flush the content to the browser
        flush();
 
        // sleep one second
        sleep(1);
    }
    // close file stream
    fclose($file);
}
else
{
    die('Error: The file '.$local_file.' does not exist!');
}
?>


'Tip & Tech > php' 카테고리의 다른 글

iframe을 이용한 중복체크  (1) 2012.04.27
간단한 PHP 파일 업로드, 다운로드 구현  (1) 2011.07.28
PHP HomePage Builder  (0) 2010.07.14
PHP HomePage Builder - 홈페이지의 구조 알기  (0) 2010.07.13
PHP Text Files 핸들링  (0) 2010.06.27

댓글