【问题标题】:Unable to read files which are greater than 2GB无法读取大于 2GB 的文件
【发布时间】:2018-09-27 06:25:12
【问题描述】:

我正在使用下面的代码在我的本地主机上播放电影。小于 2GB 的电影可以正常播放,但是当涉及大于 2GB 的电影时,电影无法播放。

请帮助我如何阅读更大的文件。

这是我用于流式传输电影的代码

<?php

//Determine file path according to extension
if (!isset($_GET['ext']) || $_GET['ext'] == 'mp4') {
    $path ='movies/'.$_GET['movie'];
}


$finfo = new finfo(FILEINFO_MIME_TYPE);
$mime = $finfo->file($path);
header('Content-Type: ' . $mime);

$size = filesize($path);


if (isset($_SERVER['HTTP_RANGE'])) {


    list($specifier, $value) = explode('=', $_SERVER['HTTP_RANGE']);
    if ($specifier != 'bytes') {
        header('HTTP/1.1 400 Bad Request');
        return;
    }


    list($from, $to) = explode('-', $value);
    if (!$to) {
        $to = $size - 1;
    }


    $fp = fopen($path, 'rb');
    if (!$fp) {
        header('HTTP/1.1 500 Internal Server Error');
        return;
    }


    header('HTTP/1.1 206 Partial Content');
    header('Accept-Ranges: bytes');


    header('Content-Length: ' . ($to - $from));


    header("Content-Range: bytes {$from}-{$to}/{$size}");


    fseek($fp, $from);


    while(true){

        if(ftell($fp) >= $to){
            break;
        }


        echo fread($fp, 8192);

        // Flush do buffer
        ob_flush();
        flush();
    }
}
else {

    header('Content-Length: ' . $size);


    readfile($path);
}

我使用 xammpp 作为服务器将文件托管在 localhost 上。

【问题讨论】:

    标签: php video-streaming streaming filereader


    【解决方案1】:

    这可能有不同的原因。

    fopen() 将文件读入您​​的 RAM。所以限制似乎来自那里。

    首先,确保您的系统中至少安装了 4GB。 如果您需要更多,请确保您不要使用 Win 32 位系统,因为它最多仅支持 4GB 内存。

    那你可以尝试增加PHP的memory_limit。打开 xampp/php/php.ini 文件并将 memory_limit 更改为您需要的任何值或直接在您的 php 文件中设置 ini 值,如下所示

    ini_set(”memory_limit”,”16M”);

    希望这会有所帮助。对于更具体的答案,如果您有任何错误消息会很有帮助。

    【讨论】:

      猜你喜欢
      • 2014-05-28
      • 1970-01-01
      • 2013-02-19
      • 2020-09-16
      • 1970-01-01
      • 1970-01-01
      • 2013-10-19
      • 1970-01-01
      • 2011-04-08
      相关资源
      最近更新 更多