2017-04-03 1 views
0

pdf 및 미디어 형식의 대용량 파일을 다운로드하는 데 사용되는 스크립트가 있습니다. 나는 그것을 다운로드 할 수 없다 때로는 내부 오류가 때로는 500 내부 서버 오류를주는 HTTP 내부 오류를 제공합니다. HTTP 힘 헤더 이후HTTP 헤더가 <2GB 파일보다 위에 있지 않음

<?php 


//The directory where the download files are kept - keep outside of the web document root 
$strDownloadFolder = "uploads/"; 

//If you can download a file more than once 
$boolAllowMultipleDownload = 0; 

// 1. Create a database connection 
//connect to the DB 
$resDB = mysql_connect("localhost", "root", ""); 
mysql_select_db("downloader", $resDB); 


if(!empty($_GET['key'])){ 
    //check the DB for the key 
    $resCheck = mysql_query("SELECT * FROM downloads WHERE downloadkey = '".mysql_real_escape_string($_GET['key'])."' LIMIT 1"); 
    if($resCheck == FALSE) { echo "QUERY FAILED: " . mysql_error(); } 
    $arrCheck = mysql_fetch_assoc($resCheck); 
    if(!empty($arrCheck['file'])){ 
     //check that the download time hasnt expired 
     if($arrCheck['expires']>=time()){ 
      if(!$arrCheck['downloads'] OR $boolAllowMultipleDownload){ 
       //everything is hunky dory - check the file exists and then let the user download it 
       $strDownload = $strDownloadFolder.$arrCheck['file']; 

       if(file_exists($strDownload)){ 

        //get the file content 
        $strFile = file_get_contents($strDownload); 

        //set the headers to force a download 
        header("Content-type: application/force-download"); 
        header("Content-Disposition: attachment; filename=\"".str_replace(" ", "_", $arrCheck['file'])."\""); 

        //echo the file to the user 
        echo $strFile; 

        //update the DB to say this file has been downloaded 
        mysql_query("UPDATE downloads SET downloads = downloads + 1 WHERE downloadkey = '".mysqli_real_escape_string($_GET['key'])."' LIMIT 1"); 

        exit; 

       }else{ 
        echo "We couldn't find the file to download."; 
       } 
      }else{ 
       //this file has already been downloaded and multiple downloads are not allowed 
       echo "This file has already been downloaded."; 
      } 
     }else{ 
      //this download has passed its expiry date 
      echo "This download has expired."; 
     } 
    }else{ 
     //the download key given didnt match anything in the DB 
     echo "No file was found to download."; 
    } 
}else{ 
    //No download key wa provided to this script 
    echo "No download key was provided. Please return to the previous page and try again."; 
} 

?> 

이 큰 파일을 작동하지 않습니다 ... 나는 사용자가 클릭 할 때 그래서 직접 암호화 된 링크를해야 속성 다운로드와 함께 사용 HREF 싶어! 토큰 또는 제한된 세션으로!

+0

는 오류 로그 봤어? 또한 mysql_ * 함수는 PHP 7에서 제거되고 PHP5.6 이후에는 사용되지 않습니다. 나중에 PDO 나 mysqli로 전환하고 나중에 두통을 줄여야합니다. – aynber

답변

1

file_get_contents은 전체 파일을 메모리로 읽습니다. 파일이 사용 가능한 메모리보다 크면 실패합니다.

대신 출력 스트림에 직접 파일 내용을 읽을 수 있습니다

//read whole file into memory, whoops 
//$strFile = file_get_contents($strDownload); 

//set the headers to force a download 
header("Content-type: application/force-download"); 
header("Content-Disposition: attachment; filename=\"".str_replace(" ", "_", $arrCheck['file'])."\""); 

//read file to output stream 
readfile($strDownload); 

http://php.net/manual/en/function.readfile.php

+0

굉장! 고맙습니다. 완벽하게 작동했습니다 ... –

+0

아무런 문제가 없지만 다행입니다. – Steve

+0

지금은 훨씬 나아졌습니다.하지만 여전히 700mb 또는 1GB 파일의 일부 파일에는 여전히 "이 사이트에 연결할 수 없습니다"라는 오류가 계속 발생합니다. 서버는 Windows 2016입니다. 원격 데스크톱을 사용할 수 있고 RAM은 8GB입니다 ... php.ini에서 3GB 메모리 제한을 사용하고 또한 php.ini에서 3GB 메모리를 사용했습니다 ... 어떤 설정을 사용해야합니까? .htaccess를 사용하는 것이 필요합니까? –

관련 문제