问题描述
我正在创建一个备份系统,备份将自动生成,因此我将把备份存储在不同的服务器上,但是当我想要下载它们时,我希望链接是一次性链接,但这不是不难做出,但为了保证安全,我正在考虑存储这些文件,以免他们通过另一台服务器上的http进行访问。
因此,我要做的是通过ftp连接,将文件下载到主服务器,然后将其下载并删除,但是这会花费很长时间备份很大,有没有一种方法可以通过FTP进行流式传输,而不需要显示下载人员的实际位置,也不需要将其存储在服务器上?
这是一个使用。它指定了一个读取回调函数,当可以从FTP读取数据时,将调用该回调函数,并将数据输出到浏览器,以在FTP备份服务器与FTP服务器进行同步下载的同时下载到客户端。
这是一个非常基本的例子,您可以扩展它。
< php
// ftp URL到文件
$ url ='ftp://ftp.mozilla.org/pub/firefox/nightly/latest-firefox-3.6.x/firefox-3.6 .29pre.en-US.linux-i686.tar.bz2' ;
//使用FTP地址初始化curl会话
$ ch = curl_init($ url);
//指定读取数据的回调函数
curl_setopt($ ch,CURLOPT_READFUNCTION,'readCallback');
//发送客户端下载头
头('Content-type:application / octet-stream');
header('Content-Disposition:attachment; filename =backup.tar.bz2');
//执行请求,当数据可用时,我们的读取回调将被调用
curl_exec($ ch);
//读取回调函数,需要3个参数,curl句柄,要读取的流以及读取的最大字节数
函数readCallback($ curl,$流,$ maxRead)
{
//从ftp流读取数据
$ read = fgets($ stream,$ maxRead);
//回显刚刚读给客户端的内容,这些内容有助于他们的总下载
echo $ read;
//返回读取的数据,使函数继续运行
return $ read;
}
请参阅获取更多关于 CURLOPT_READFUNCTION
选项的信息。
I'm creating a backup system, where backups will be generated automaticly, so I will be storing backups on a different server, however when I want to download them, I want the link to be a one time link, this isn't hard to make, however to make this secure I was thinking about storing the files so their not accesible via http on the other server.
So what I would do is connet via ftp, download the file to the main server, then present it for download and deleteit, however this will take a long time if the backup is large, is there a way to stream it from FTP without showing the person who is downloadiong the actual location and not store it on the server?
Here is a very basic example using cURL. It specifies a read callback which will be called when data is available to be read from FTP, and outputs the data to the browser to serve a simultaneous download to the client while the FTP transaction is taking place with the backup server.
This is a very basic exmaple which you can expand on.
<?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;
}
See curl_setopt() for more info on the CURLOPT_READFUNCTION
option.
这篇关于从FTP传输文件并让用户同时下载它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!