2012-04-22 64 views
2

我正在创建备份系统,备份将自动生成,因此我将备份存储在不同的服务器上,但是当我想要下载它们时,我想链接是一次性链接,这并不难,但是为了保证安全,我正在考虑存储这些文件,以免它们通过另一台服务器上的http访问。从FTP传输文件并让用户同时下载它

所以我会做的是通过ftp connet,将文件下载到主服务器,然后提交它下载和删除,但是这将需要很长时间,如果备份很大,是否有一种方法来流从FTP没有显示下载人的实际位置,而不是将其存储在服务器上?

+0

是这些内部备份吗?谁在做下载? – 2012-04-22 22:25:32

回答

0

这是一个使用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()