PHP - 下载大文件的安全方式?

3

信息

在PHP中,有许多下载文件的方法,包括file_get_contents+file_put_contentsfopenreadfile和cURL。

问题?

  • 当从另一个服务器/域下载大文件(比如500MB)时,什么是“正确”的安全下载方式?如果连接失败,它应该找到位置并继续下载,或者如果文件包含错误,则重新下载。
  • 它将在网站上使用,而不是在php.exe shell中。

目前我了解到的情况

  • 我已经阅读了关于带有进度条的AJAX解决方案,但我真正寻找的是PHP解决方案。
  • 我不需要像file_get_contents那样将文件缓冲到字符串中,因为这可能也会使用内存。
  • 我也阅读了有关内存问题的内容。一个不使用太多内存的解决方案可能更受欢迎。

概念

如果结果为false,则这就是我想要的东西。

function download_url( $url, $filename ) {
    // Code
    $success['success'] = false;
    $success['message'] = 'File not found';
    return $success;
}

我个人使用cURL取得了不错的结果,你是从远程位置下载还是从与你的脚本相同的服务器下载? - naththedeveloper
1
我也经常使用cURL。我知道还有其他的工具,但考虑到cURL扩展的功能集,使用它们似乎有些愚蠢。此外,这个下载操作是在处理HTTP请求的上下文中进行的,还是从命令行调用或以其他方式生成的进程中进行的呢? - prodigitalson
此外,还有很多其他的SO 答案 可供参考 关于此问题。 - naththedeveloper
@FDL 来自另一个服务器/域名。 - Jens Törnell
@prodigitalson 这将用于网站(后端)。我现在已经更新了我的问题,加入了这个信息。 - Jens Törnell
可能是[如何使用PHP(低内存使用)下载大文件]的重复问题(https://dev59.com/Tm865IYBdhLWcg3wEaYx)。 - PeeHaa
2个回答

3

这里展示了一种复制大文件的简单方法Save large files from php stdin,但是它并没有展示如何使用http range复制文件。

$url = "http://REMOTE_FILE";
$local = __DIR__ . "/test.dat";

try {
    $download = new Downloader($url);
    $download->start($local); // Start Download Process
} catch (Exception $e) {
    printf("Copied %d bytes\n", $pos = $download->getPos());
}

当发生异常时,您可以从上次下载点恢复文件下载。
$download->setPos($pos);

使用的类

class Downloader {
    private $url;
    private $length = 8192;
    private $pos = 0;
    private $timeout = 60;

    public function __construct($url) {
        $this->url = $url;
    }

    public function setLength($length) {
        $this->length = $length;
    }

    public function setTimeout($timeout) {
        $this->timeout = $timeout;
    }

    public function setPos($pos) {
        $this->pos = $pos;
    }

    public function getPos() {
        return $this->pos;
    }

    public function start($local) {
        $part = $this->getPart("0-1");

        // Check partial Support
        if ($part && strlen($part) === 2) {
            // Split data with curl
            $this->runPartial($local);
        } else {
            // Use stream copy
            $this->runNormal($local);
        }
    }

    private function runNormal($local) {
        $in = fopen($this->url, "r");
        $out = fopen($local, 'w');
        $pos = ftell($in);
        while(($pos = ftell($in)) <= $this->pos) {
            $n = ($pos + $this->length) > $this->length ? $this->length : $this->pos;
            fread($in, $n);
        }
        $this->pos = stream_copy_to_stream($in, $out);
        return $this->pos;
    }

    private function runPartial($local) {
        $i = $this->pos;
        $fp = fopen($local, 'w');
        fseek($fp, $this->pos);
        while(true) {
            $data = $this->getPart(sprintf("%d-%d", $i, ($i + $this->length)));

            $i += strlen($data);
            fwrite($fp, $data);

            $this->pos = $i;
            if ($data === - 1)
                throw new Exception("File Corupted");

            if (! $data)
                break;
        }

        fclose($fp);
    }

    private function getPart($range) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_RANGE, $range);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
        $result = curl_exec($ch);
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        // Request not Satisfiable
        if ($code == 416)
            return false;

            // Check 206 Partial Content
        if ($code != 206)
            return - 1;

        return $result;
    }
}

我无法使用您的下载器下载以下文件http://www.biart7.com/dh_demo.zip。我收到了“文件损坏”异常。 - alexanoid
很可能你的服务器不支持范围。 - Baba
1
这种方法可以工作,但速度非常慢。$length是什么意思?有没有办法超级加速它?我正在使用共享服务器。它大约有1GB的内存。 - shakee93

1

1
这应该是一条评论和/或关闭重复投票。 - deceze
你能详细说明为什么人们总是希望分块下载吗? - bart

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接