2012-07-06 67 views
7

我想用Curl下載一個文件。 問題是,下載鏈接是不是直接的,例如:下載文件用url捲曲var

http://localhost/download.php?id=13456 

當我嘗試下載與捲曲的文件,它下載文件的download.php!

這裏是我的捲曲代碼:

 ### 
     function DownloadTorrent($a) { 
        $save_to = $this->torrentfolder; // Set torrent folder for download 
        $filename = str_replace('.torrent', '.stf', basename($a)); 

        $fp = fopen ($this->torrentfolder.strtolower($filename), 'w+');//This is the file where we save the information 
        $ch = curl_init($a);//Here is the file we are downloading 
        curl_setopt($ch, CURLOPT_ENCODING, "gzip"); // Important 
        curl_setopt($ch, CURLOPT_TIMEOUT, 50); 
        curl_setopt($ch, CURLOPT_URL, $fp); 
        curl_setopt($ch, CURLOPT_HEADER,0); // None header 
        curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); // Binary trasfer 1 
        curl_exec($ch); 
        curl_close($ch); 
        fclose($fp); 
    } 

有沒有一種方法來下載文件,但不知道的路徑?

回答

4

您可以嘗試CURLOPT_FOLLOWLOCATION

TRUE遵循任何「位置:」服務器發送的HTTP標頭的一部分 (注意這是遞歸的,PHP將遵循儘可能多的 頭「的位置:」它發送的標題,除非CURLOPT_MAXREDIRS是 set)。

因此,這將導致到:

function DownloadTorrent($a) { 
    $save_to = $this->torrentfolder; // Set torrent folder for download 
    $filename = str_replace('.torrent', '.stf', basename($a)); 

    $fp = fopen ($this->torrentfolder.strtolower($filename), 'w+');//This is the file where we save the information 
    $ch = curl_init($a);//Here is the file we are downloading 
    curl_setopt($ch, CURLOPT_ENCODING, "gzip"); // Important 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 50); 
    curl_setopt($ch, CURLOPT_FILE, $fp); 
    curl_setopt($ch, CURLOPT_HEADER,0); // None header 
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1); // Binary transfer 1 
    curl_exec($ch); 
    curl_close($ch); 
    fclose($fp); 
} 
1

哦!

CURLOPT_FOLLOWLOCATION工作完美...

的問題是,我使用CURLOPT_URL爲fopen()函數,我簡單地改變CURLOPT_URL絲毫CURLOPT_FILE

和它工作得很好! 感謝您的幫助=)