doujiang2643 2014-10-22 08:17
浏览 97

PHP feof()下载 - 网络错误

I have a bunch of files available for download that I wanted to protect by login and hide the path to prevent hotlinking. I am using a PHP script to do this (Thanks to Mike Zriel for the download script, I have simply added my own database call and user login check).

/**
* Force file download and hide real Path
* @version        11.03.11 March 11, 2011
* @author         Mike Zriel, http://www.zriel.com
* @copyright      Copyright (C) 2010
* @license        http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
* @params     
*   filePath = Real Path of file
*   fileName = File Name
*/

//CHECK USER LOGIN
if(!isset($_COOKIE['login'])) {
echo "You are not authorised to download this file.";
exit;
} else {

include('database_connection.php');

//VALIDATE VARIABLES
if(isset($_GET['fileid'])) {
    if(!preg_match("/^\d+$/",$_GET['fileid'])) {
        echo "Invalid File ID.";
        exit;
    }
} else {
    echo "No File Specified.";
    exit;
}

try {
    $sql = $pdo->prepare("SELECT * FROM files WHERE id = ?");
    $sql->execute(array($_GET['fileid']));
    $array = $sql->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    echo "Error downloading file: ".$e->getCode();
}

if(!empty($array)) {
    $filePath = "http://www.example.com/PathToFile/";
    $fileName = $array['path']);
}

if(substr($filePath,-1)!="/") $filePath .= "/";

$pathOnHd = $filePath . $fileName;

if(isset($_GET['debug'])) {
echo "<br />".$pathOnHd;
}

if ($download = fopen ($pathOnHd, "br")) {

$size = filesize($pathOnHd);
$fileInfo = pathinfo($pathOnHd);
$ext = strtolower($fileInfo["extension"]);

switch ($ext) { 
case "pdf": 
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=\"{$fileInfo["basename"]}\"");
break;
default;
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"{$fileInfo["basename"]}\"");
}
header("Content-length: $size");

while(!feof($download)) {
    $buffer = fread($download, 2048);
    echo $buffer;
}
fclose ($download);
} else {
    echo "There was an error accessing the file: ".$array['name'].". <br />";
}
exit;
}

The problem I am having is for some o the smaller ZIP or PDF files (<1MB or so) this works fine, but for some larger ZIP files I have (15-20MB) the browser (tested in Chrome and Firefox) throws a network error and fails at the end of the download. I think it has something to do with this bit but changing the buffer size doesn't seem to have any effect?

while(!feof($download)) {
    $buffer = fread($download, 2048);
    echo $buffer;
}

Can anyone spot what's wrong?

Edit: Tried the following from answers below...

readfile($pathOnHd); //Results in Unknown Network Error

while(!feof($download)) {
$buffer = fread($download, 2048);
echo $buffer;
flush();
}   //Not using ob_start() so not sure why this would change anything and it doesn't

while (($buffer = fread($download, 2048)) != FALSE) {
echo $buffer;
// Results in Unknown Network Error
}

Note: If I echo the path to the browser and paste it in as a direct link the file downloads ok. So I's something to do with PHP not liking these larger files.

  • 写回答

3条回答 默认 最新

  • dousi9215 2014-10-22 08:27
    关注

    Instead of reading the file in little blocks try using the readfile() function instead. This will read the whole file in one go.

    So change this

    while(!feof($download)) {
        $buffer = fread($download, 2048);
        echo $buffer;
    }
    

    To

    readfile($pathOnHd)
    

    You can also remove the fopen() as this is not required as readfile() opens and closes the file automatically.

    See the manual for a great example that is also relevant to what you are doing

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘