2012-07-06 33 views
7

Tôi muốn tải xuống tệp có Curl. Vấn đề là liên kết tải xuống không được trực tiếp, ví dụ:Tải xuống tệp Curl với url var

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

Khi tôi cố gắng để tải về các tập tin với curl, nó tải file download.php!

Đây là mã curl của tôi:

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

Có cách nào để tải các tập tin mà không biết con đường?

Trả lời

4

Bạn có thể thử CURLOPT_FOLLOWLOCATION

TRUE để làm theo bất kỳ "Location:" tiêu đề mà máy chủ gửi như là một phần của tiêu đề HTTP (lưu ý đây là đệ quy, PHP sẽ làm theo như nhiều "Location:" tiêu đề được gửi đi, trừ khi CURLOPT_MAXREDIRS là bộ).

Vì vậy, nó sẽ cho kết quả thành:

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

Oooh!

CURLOPT_FOLLOWLOCATION công việc hoàn hảo ...

Vấn đề là tôi sử dụng CURLOPT_URL cho fopen(), tôi chỉ cần thay đổi CURLOPT_URL whit CURLOPT_FILE

và nó hoạt động rất tốt! cảm ơn sự giúp đỡ của bạn =)

Các vấn đề liên quan