如何使用PHP下载大文件(内存占用低)

2022-01-02 00:00:00 file download php

我必须使用 PHP 下载大文件 (1xx MB).

I have to download big file (1xx MB) using PHP.

如何在不浪费内存 (RAM) 的情况下下载临时文件?

How can i download this without wasting memory (RAM) for temporary file ?

当我使用

$something=file_get_contents('http://somehost.example/file.zip');
file_put_contents($something,'myfile.zip');

我需要有那个文件那么大的内存.

I need to have so much memory that size of that file.

也许可以使用任何其他方式下载它?

Maybe it's possible to download it using any other way ?

例如在部分(例如 1024b)中,写入磁盘,然后重复下载另一部分直到文件完全下载?

For example in parts (for example 1024b), write to disk, and download another part repeating until file will be fully downloaded ?

推荐答案

一次一小块地复制文件

/**
 * Copy remote file over HTTP one small chunk at a time.
 *
 * @param $infile The full URL to the remote file
 * @param $outfile The path where to save the file
 */
function copyfile_chunked($infile, $outfile) {
    $chunksize = 10 * (1024 * 1024); // 10 Megs

    /**
     * parse_url breaks a part a URL into it's parts, i.e. host, path,
     * query string, etc.
     */
    $parts = parse_url($infile);
    $i_handle = fsockopen($parts['host'], 80, $errstr, $errcode, 5);
    $o_handle = fopen($outfile, 'wb');

    if ($i_handle == false || $o_handle == false) {
        return false;
    }

    if (!empty($parts['query'])) {
        $parts['path'] .= '?' . $parts['query'];
    }

    /**
     * Send the request to the server for the file
     */
    $request = "GET {$parts['path']} HTTP/1.1
";
    $request .= "Host: {$parts['host']}
";
    $request .= "User-Agent: Mozilla/5.0
";
    $request .= "Keep-Alive: 115
";
    $request .= "Connection: keep-alive

";
    fwrite($i_handle, $request);

    /**
     * Now read the headers from the remote server. We'll need
     * to get the content length.
     */
    $headers = array();
    while(!feof($i_handle)) {
        $line = fgets($i_handle);
        if ($line == "
") break;
        $headers[] = $line;
    }

    /**
     * Look for the Content-Length header, and get the size
     * of the remote file.
     */
    $length = 0;
    foreach($headers as $header) {
        if (stripos($header, 'Content-Length:') === 0) {
            $length = (int)str_replace('Content-Length: ', '', $header);
            break;
        }
    }

    /**
     * Start reading in the remote file, and writing it to the
     * local file one chunk at a time.
     */
    $cnt = 0;
    while(!feof($i_handle)) {
        $buf = '';
        $buf = fread($i_handle, $chunksize);
        $bytes = fwrite($o_handle, $buf);
        if ($bytes == false) {
            return false;
        }
        $cnt += $bytes;

        /**
         * We're done reading when we've reached the conent length
         */
        if ($cnt >= $length) break;
    }

    fclose($i_handle);
    fclose($o_handle);
    return $cnt;
}

根据您的需要调整 $chunksize 变量.这只是经过了温和的测试.由于多种原因,它很容易损坏.

Adjust the $chunksize variable to your needs. This has only been mildly tested. It could easily break for a number of reasons.

用法:

copyfile_chunked('http://somesite.com/somefile.jpg', '/local/path/somefile.jpg');

相关文章