2012-04-22 32 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()