这是一个使用cURL的非常基本的示例。它指定了一个读取回调函数,当数据可用于从FTP读取数据时将会调用该回调函数,并将数据输出到浏览器,以在FTP备份与备份服务器发生同步下载时提供给客户机。
这是一个非常基本的例子,你可以扩展。
<?php
// ftp URL to file
$url = 'ftp://ftp.mozilla.org/pub/firefox/nightly/latest-firefox-3.6.x/firefox-3.6.29pre.en-US.linux-i686.tar.bz2';
// init curl session with FTP address
$ch = curl_init($url);
// specify a callback function for reading data
curl_setopt($ch, CURLOPT_READFUNCTION, 'readCallback');
// send download headers for client
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename="backup.tar.bz2"');
// execute request, our read callback will be called when data is available
curl_exec($ch);
// read callback function, takes 3 params, the curl handle, the stream to read from and the maximum number of bytes to read
function readCallback($curl, $stream, $maxRead)
{
// read the data from the ftp stream
$read = fgets($stream, $maxRead);
// echo the contents just read to the client which contributes to their total download
echo $read;
// return the read data so the function continues to operate
return $read;
}
有关CURLOPT_READFUNCTION
选项的详细信息,请参阅curl_setopt()。
是这些内部备份吗?谁在做下载? – 2012-04-22 22:25:32