Php http强制标头不适用于以上内容<;2GB文件

Php http强制标头不适用于以上内容<;2GB文件,php,Php,我有一个脚本,用于下载pdf和媒体格式的大文件。我不能下载它,它有时给http内部错误,有时给500内部服务器错误 <?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 $boolAllowMul

我有一个脚本,用于下载pdf和媒体格式的大文件。我不能下载它,它有时给http内部错误,有时给500内部服务器错误

<?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.";
}

?>

文件获取内容
将整个文件读取到内存中。如果文件大于可用内存,则会失败

相反,您可以将文件内容直接读取到输出流:

//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);

您是否查看了错误日志?此外,PHP 7中删除了mysql_*函数,PHP5.6之后不再推荐使用。你现在应该改用PDO或mysqli,以后不要再头痛了。没问题,很高兴我能帮你。现在好多了。。。但在一些700mb或1gb的文件上,我在下载“无法访问此站点”时仍然收到此错误。我的服务器是windows 2016,它启用了远程桌面,RAM是8gb。。。在php.ini中,我使用了3GB内存限制,在php.ini中还使用了post内存3GB。。。。您建议我使用什么设置?而使用.htaccess是必要的吗?不幸的是,我最近对windows server几乎没有经验,而且我以前在纯c#net而非php的情况下使用过它。作为第一步,打开错误报告并检查日志这是我的日志显示的内容。。。请指导我该怎么做?这是我的php内存设置。。。请帮我解决这个问题,我会付钱的@史蒂夫