2017-04-03 8 views
0

私は、大きなファイルをpdf形式とメディア形式でダウンロードするためのスクリプトを用意しています。私はそれをダウンロードすることはできませんそれは時々500の内部サーバーのエラーを与えるときにHTTP内部エラーを与える。http forceヘッダーが上記の<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."; 
} 

?> 

HTTP力ヘッダーは大きなファイルのために働いていないので...私は、ユーザーがそれをクリックしたときに、それが直接暗号化されたリンクを持っている必要があり、ダウンロード属性を持つ利用の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を使用する必要はありますか? –

関連する問題