Php 使用curl下载大文件

Php 使用curl下载大文件,php,curl,download,Php,Curl,Download,我需要使用curl下载远程文件 以下是我的示例代码: $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $st = curl_exec($ch); $fd = fopen($tmp_name, 'w'); fwrite($fd, $st); fclose($fd); curl_close($ch); 但它不能处理大文件,因为它首先读取内存

我需要使用curl下载远程文件

以下是我的示例代码:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$st = curl_exec($ch);
$fd = fopen($tmp_name, 'w');
fwrite($fd, $st);
fclose($fd);

curl_close($ch);
但它不能处理大文件,因为它首先读取内存


是否可以将文件直接流式传输到磁盘?

我使用这个方便的功能:

通过下载4094字节的步骤,它不会填满你的内存

function download($file_source, $file_target) {
    $rh = fopen($file_source, 'rb');
    $wh = fopen($file_target, 'w+b');
    if (!$rh || !$wh) {
        return false;
    }

    while (!feof($rh)) {
        if (fwrite($wh, fread($rh, 4096)) === FALSE) {
            return false;
        }
        echo ' ';
        flush();
    }

    fclose($rh);
    fclose($wh);

    return true;
}
用法:

     $result = download('http://url','path/local/file');
然后,您可以检查以下各项是否正常:

     if (!$result)
         throw new Exception('Download error...');

如果要下载指定URL的内容,也要将其保存到文件中,请查找下面的代码

<?php
$ch = curl_init();
/**
* Set the URL of the page or file to download.
*/
curl_setopt($ch, CURLOPT_URL,'http://news.google.com/news?hl=en&topic=t&output=rss');

$fp = fopen('rss.xml', 'w+');
/**
* Ask cURL to write the contents to a file
*/
curl_setopt($ch, CURLOPT_FILE, $fp);

curl_exec ($ch);

curl_close ($ch);
fclose($fp);
?>

如果要从FTP服务器下载文件,可以使用php FTP扩展名。请查找以下代码:

<?php
$SERVER_ADDRESS="";
$SERVER_USERNAME="";
$SERVER_PASSWORD="";
$conn_id = ftp_connect($SERVER_ADDRESS);

// login with username and password
$login_result = ftp_login($conn_id, $SERVER_USERNAME, $SERVER_PASSWORD);

$server_file="test.pdf" //FTP server file path 
$local_file = "new.pdf"; //Local server file path 

##----- DOWNLOAD $SERVER_FILE AND SAVE TO $LOCAL_FILE--------##
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) {
    echo "Successfully written to $local_file\n";
} else {
    echo "There was a problem\n";
}

ftp_close($conn_id);
?>

当使用
curl
下载大文件时,您必须设置的主要选项是
CURLOPT\u TIMEOUT

CURLOPT_RETURNTRANSFER
必须为真,以防收到pdf/csv/image等文件

您可以在这里找到更多详细信息(正确的url)

从该页:

curl_setopt($request, CURLOPT_TIMEOUT, 300); //set timeout to 5 mins

curl_setopt($request, CURLOPT_RETURNTRANSFER, true); // true to get the output as string otherwise false

您可以使用此函数,它在文件系统中创建一个tempfile,并在一切正常的情况下返回下载文件的路径:

function getFileContents($url)
{
    // Workaround: Save temp file
    $img = tempnam(sys_get_temp_dir(), 'pdf-');
    $img .= '.' . pathinfo($url, PATHINFO_EXTENSION);

    $fp = fopen($img, 'w+');

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    $result = curl_exec($ch);
    curl_close($ch);

    fclose($fp);

    return $result ? $img : false;
}

为你的评论辩护@yes123,我很想知道。如果我错了,请纠正我,但我认为你实际上不需要手动
fwrite
数据,因为你使用的是
CURLOPT_文件
。正如@SashaChedygov上面指出的,你不需要使用
fwrite
CURLOPT_文件
。传递
$fp
就足够了。我同时做了这两件事,结果在文件内容的末尾出现了
1
。@Sasha Chedygov~是的,您不需要
fwrite
在设置CURLOPT\u RETURNTRANSFER之前设置CURLOPT\u文件似乎不起作用,大概是因为CURLOPT\u文件取决于设置的CURLOPT\u RETURNTRANSFER@Severus如果您发现http错误为
fopen()
返回false并超时,您将其放入while循环(调用
time()
并进行计算)cURL已经有了这方面的工作实现(参见接受的答案),为什么您要自己实现某个东西?因为cURL过程接口的价值非常低,我一直在使用
stream\u copy\u to\u stream
而不是手动复制内容,因此代码更短。这个和那个都不适用于
https
(除非您规定了
$context
)。协调过程式-文件函数也不是很OOP,如果你在数组中加入curl选项,它看起来会更干净。我用https测试了它,效果很好!!!,感谢@dynamic提供的帮助。您还可以浏览有关使用curl下载文件的博客示例
function getFileContents($url)
{
    // Workaround: Save temp file
    $img = tempnam(sys_get_temp_dir(), 'pdf-');
    $img .= '.' . pathinfo($url, PATHINFO_EXTENSION);

    $fp = fopen($img, 'w+');

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

    $result = curl_exec($ch);
    curl_close($ch);

    fclose($fp);

    return $result ? $img : false;
}