2014-10-01 2 views
2

리디렉션없이 파일을 다운로드하거나 다른 파일을 사용하여 큰 zip 파일을 다운로드하려는 링크를 얻으려고합니다.큰 파일에서 PHP 다운로드가 실패합니다.

이 파일은 ~ 100MB 미만의 파일에 적합하지만 그 이상인 파일은 Error code: ERR_INVALID_RESPONSE이라는 크롬의 오류 페이지로 전송됩니다.

$ file 및 $ path는 jQuery와 함께 양식에 넣어지고 $('form').submit();이 호출됩니다.

function downloadFile($file, $path){ 

     set_time_limit(0); 
     $fullPath = $path . $file; 

     if ($fd = fopen ($fullPath, "r")) { 


      $fsize = filesize($fullPath); 
      $path_parts = pathinfo($fullPath); 



      $ext = strtolower($path_parts["extension"]); 
      switch ($ext) { 
       case "pdf": 
       header("Content-type: application/pdf"); // add here more headers for diff. extensions 
       header("Content-Disposition: attachment; filename=\"".$path_parts["basename"]."\""); // use 'attachment' to force a download 
       break; 
       default; 
       header("Content-type: application/octet-stream"); 
       header("Content-Disposition: filename=\"".$path_parts["basename"]."\""); 
      } 
      header("Content-length: $fsize"); 
      header("Cache-control: private"); //use this to open files directly 
      while(!feof($fd)) { 
       $buffer = fread($fd, 2048); 
       echo $buffer; 
      } 
     } 
     fclose ($fd); 
     exit; 
    } 

대용량 파일에서 실패한 이유는 무엇입니까?

업데이트 : 나는 다음과 같은 코드를 사용하면 , 그것은 파일을 다운로드하지만, 어떤 이유로 손상됩니다으로 완성 된 파일이 열리지 않습니다 :

set_time_limit(0); 
ini_set('memory_limit', '1024M'); 
// http headers for downloads 
header("Pragma: public"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Cache-Control: public"); 
header("Content-Description: File Transfer"); 
header("Content-type: application/octet-stream"); 
header("Content-Disposition: attachment; filename=\"".$filename."\""); 
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: ".$file_size); 
ob_end_flush(); 
@readfile($fileinfo['path']); 

이 코드를 사용, 그것은 단, 첫 번째 49.7kb 다음이 완료 말한다 다운로드 난 함께 ob_flush()flush()을 시도 :

// http headers for downloads 
    header("Pragma: public"); 
    header("Expires: 0"); 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    header("Cache-Control: public"); 
    header("Content-Description: File Transfer"); 
    header("Content-type: application/octet-stream"); 
    header("Content-Disposition: attachment; filename=\"".$filename."\""); 
    header("Content-Transfer-Encoding: binary"); 
    header("Content-Length: ".filesize($fileinfo['path'])); 

    if ($fd = fopen ($fileinfo['path'], "r")) { 

      set_time_limit(0); 
      ini_set('memory_limit', '1024M'); 

     while(!feof($fd)) { 
      echo fread($fd, 4096); 
      flush(); 
     } 

    } 
+1

최대 업로드 크기 제한이 php.ini 파일에 있는지 확인하십시오. –

+0

그게 문제인지 잘 모르겠 으면 내 업데이트를 살펴보십시오. – David

답변

4

내 다운로더에서 내가 다른 할 유일한 것은입니다. 또한 루프에서 flush()을 가져 와서 데이터를 보낸 후 ob_clean(); flush();ob_end_flush();을 수행했는지 확인하십시오.

+0

ob_clean(); 플러시(); 잠시 또는 잠시 전에? – David

+0

'while'앞에 ... – MBaas

+0

좋습니다는 잘 작동합니다. 그것이 열려 있으면 콘솔에 횡설수설 소리를 내뿜는 사실을 뺀다. echo fread ($ fd, 4096)이 원인이되지만 제거하면 작동하지 않습니다. – David

관련 문제