2012-07-06 38 views
7

私はCurlでファイルをダウンロードしたいと思います。私はカールを使用してファイルをダウンロードしようとすると、それはファイルdownload.phpをファイルをダウンロードURLを使ってCurl

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

ダウンロード: 問題は、たとえば、直接ダウンロードリンクがないということです!

ここに私のカールコードです:

 ### 
     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); 
    } 

パスを知らなくても、ファイルをダウンロードする方法はありますか?サーバがHTTPヘッダの一部 として送信ヘッダ(PHPは、できるだけ多く 従います、これは再帰的であることに注意「場所:」:

答えて

4

あなたは、任意の「場所」に従うためにCURLOPT_FOLLOWLOCATION

TRUEを試すことCURLOPT_MAXREDIRSが に設定されていない限り、送信されるヘッダー)。

だから、になります:例えば、

trueにFOLLOWLOCATIONオプションを設定
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聖霊降臨祭

が、それは非常にうまく機能CURLOPT_FILE変更)(fopenのためCURLOPT_URLを使用するということです! ありがとう=)

関連する問題