使用curl下载大文件

kus*_*agi 84 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);
Run Code Online (Sandbox Code Playgroud)

但它无法处理大文件,因为它首先读取内存.

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

The*_*ain 163

<?php
set_time_limit(0);
//This is the file where we save the    information
$fp = fopen (dirname(__FILE__) . '/localfile.tmp', 'w+');
//Here is the file we are downloading, replace spaces with %20
$ch = curl_init(str_replace(" ","%20",$url));
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
// write curl response to file
curl_setopt($ch, CURLOPT_FILE, $fp); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// get curl response
curl_exec($ch); 
curl_close($ch);
fclose($fp);
?>
Run Code Online (Sandbox Code Playgroud)

  • 如果我错了,请纠正我,但我认为你实际上不需要手动`fwrite`数据,因为你正在使用`CURLOPT_FILE`. (8认同)
  • 捍卫你的评论@ yes123,我很想知道. (5认同)
  • 看来在设置CURLOPT_RETURNTRANSFER之前设置CURLOPT_FILE不起作用,可能是因为CURLOPT_FILE依赖于设置的CURLOPT_RETURNTRANSFER.http://php.net/manual/en/function.curl-setopt.php#99082 (5认同)
  • @DonovanP 不,你不应该,如果你需要添加这个,这只意味着你的证书无效并且你使用的 https 错误 (3认同)

dyn*_*mic 25

我用这个方便的功能:

通过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;
}
Run Code Online (Sandbox Code Playgroud)

用法:

     $result = download('http://url','path/local/file');
Run Code Online (Sandbox Code Playgroud)

然后,您可以检查一切是否正常:

     if (!$result)
         throw new Exception('Download error...');
Run Code Online (Sandbox Code Playgroud)

  • cURL已经有了这方面的工作实现(参见接受的答案),你为什么要自己实现一些东西呢? (2认同)
  • 因为cURL程序界面非常糟糕 (2认同)

San*_*han 6

如果要下载指定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);
?>
Run Code Online (Sandbox Code Playgroud)

如果要从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);
?>
Run Code Online (Sandbox Code Playgroud)


Pra*_*ant 5

curl用于下载大文件时,则CURLOPT_TIMEOUT是您必须设置的主要选项。

CURLOPT_RETURNTRANSFER 如果您正在获取 pdf/csv/image 等文件,则必须为真。

您可以在此处找到更多详细信息(正确的网址)Curl Doc

从该页面:

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
Run Code Online (Sandbox Code Playgroud)