2011-02-14 2 views
7

내 클라이언트가 웹 사이트를 다소 멋진 서버에서 ...로 이동 시키기로 결정한 경우 ... 덜 훌륭한 서버라고 부릅니다.PHP에서 파일 다운로드, 메모리 제한 문제가 있습니까?

문제가

는 또한 더욱 어렵게 나를 위해, 그들은 fopen을 허용하지 않습니다하려면 32입니다 ...

을 다운로드 할 40MBs으로 파일 서버의 메모리 제한이 파일 크기를 20MB로 줄이면 제대로 작동합니다.

내 질문은, 내가 할 수있는 - 파일 크기를 줄이는 것 외에도 -이 작품을 만들 수 있습니까?

당신에게

편집 감사합니다`

 $fsize = filesize($file_path); 
     $path_parts = pathinfo($file_path); 
     $ext = strtolower($path_parts["extension"]); 


     switch ($ext) { 
      case "pdf": $ctype = "application/pdf"; 
       break; 
      case "exe": $ctype = "application/octet-stream"; 
       break; 
      case "zip": $ctype = "application/zip"; 
       break; 
      case "doc": $ctype = "application/msword"; 
       break; 
      case "xls": $ctype = "application/vnd.ms-excel"; 
       break; 
      case "ppt": $ctype = "application/vnd.ms-powerpoint"; 
       break; 
      case "gif": $ctype = "image/gif"; 
       break; 
      case "png": $ctype = "image/png"; 
       break; 
      case "jpeg": 
      case "jpg": $ctype = "image/jpg"; 
       break; 
      default: $ctype = "application/force-download"; 
     } 

     header("Pragma: public"); // required 
     header("Expires: 0"); 
     header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
     header("Cache-Control: private", false); // required for certain browsers 
     header("Content-Type: $ctype"); 
     header("Content-Disposition: attachment; filename=\"" . basename($file_path) . "\";"); 
     header("Content-Transfer-Encoding: binary"); 
     header("Content-Length: " . $fsize); 
     ob_clean(); 
     flush(); 
     readfile($file_path);` 

내가 php.net

// If it's a large file, readfile might not be able to do it in one go, so: 
$chunksize = 1 * (1024 * 1024); // how many bytes per chunk 
if ($size > $chunksize) { 
    $handle = fopen($realpath, 'rb'); 
    $buffer = ''; 
    while (!feof($handle)) { 
    $buffer = fread($handle, $chunksize); 
    echo $buffer; 
    ob_flush(); 
    flush(); 
    } 
    fclose($handle); 
} else { 
    readfile($realpath); 
} 
+0

phpinfo의 핵심 부분은 어떤 모양입니까? 붙여 줄래? 빠른 수정을 원할 경우 .htaccess 파일을 사용하여 PHP 설정을 변경할 수 있습니다. 그것들은 드루팔 (drupal) 문서이지만, 드루팔 (drupal)과 관련이 없습니다. http://drupal.org/node/29268 및 http://drupal.org/node/97193 – KyleWpppd

+0

특히 어떤 부분입니까? (여기에 모든 것을 게시하는 것이 다소 중요합니다.) –

+0

php.net의 코드가 잘 수행되어야합니다. 그냥 사용하지 않으면 ob_ ..을 제거하십시오. ob 인 경우 gz를 추가하여 빠른 다운로드를 고려하십시오. – Teson

답변

3

사용 readfile() 대신에 본 코드입니다. 파일을 작은 청크로 스트리밍하고 모든 백그라운드 작업을 처리하여 메모리 사용을 최소화합니다.

+0

글쎄, 나는 이미 readfile() 함수를 사용하고있다. :/나는 fopen 함수를 언급했다. 왜냐하면 나는 php.net에서 광산처럼 상황을 처리하기 위해 읽었다. 나는 fopen 함수와 버퍼를 사용해야한다. 나는 내 코드를 게시 할 것이다. –

+0

나는 문서에서 아무것도 찾을 수 없다. 'readfile'은 파일 청크를 버퍼링 할 것을 제안합니다. 정교한 케어? – Phil

+0

정말 내 문제를 해결하지 못해서 대답을 "수락"할 수 없지만 유용하다고 생각했습니다. 고맙습니다. –