2013-02-26 127 views
2

我想通過cURL從ftp服務器下載文件。 這裏是我的功能:通過cURL下載文件時出錯

function getFile($remotefolder, $remotefile, $localfile) { 
    $url = "ftp://xxxxx.de/" . $remotefolder . "/" . $remotefile; 
    $fp = fopen($localfile, "w+"); 

    $handle = curl_init(); 
    curl_setopt($handle, CURLOPT_URL, $url); 
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, 0); 
    curl_setopt($handle, CURLOPT_UPLOAD, 0); 
    curl_setopt($handle, CURLOPT_FILE, $fp); 
    curl_setopt($handle, CURLOPT_USERPWD, "<Username>:<Password>"); 
    $result = curl_exec($handle); 

    $info = curl_getinfo($handle); 

    curl_close($handle); 
    fclose($fp); 

    print_r($info); 
} 

如果我執行的是在我的電腦一切正常。但是,當我跑在服務器上的代碼,它gaves我下面的輸出和文件未下載:

output server 
============= 
Array 
(
    [url] => ftp://xxxx/xxxx/xxx.log.gz 
    [content_type] => 
    [http_code] => 257 
    [header_size] => 0 
    [request_size] => 0 
    [filetime] => -1 
    [ssl_verify_result] => 0 
    [redirect_count] => 0 
    [total_time] => 1.357453 
    [namelookup_time] => 0.002011 
    [connect_time] => 0.011153 
    [pretransfer_time] => 1.357439 
    [size_upload] => 0 
    [size_download] => 0 
    [speed_download] => 0 
    [speed_upload] => 0 
    [download_content_length] => 0 
    [upload_content_length] => 0 
    [starttransfer_time] => 0 
    [redirect_time] => 0 
) 

如果你請的更多信息,請讓我知道。

由於提前,

丹尼斯

+0

有你看着http://stackoverflow.com/questions/1178425/download-a-file- from-ftp-using-curl-and-php – sanj 2013-02-26 10:57:15

+0

是的,我讀過這篇文章,並提出了發佈codesnippet。我的問題是,這完全適用於我的測試機,但不在我的服務器上。我不知道爲什麼http代碼是257(在我的testmachine代碼是226,這是正確的)。 – DM1986 2013-02-26 11:00:52

+0

如果它是共享主機,那麼它們的設置可能有一些限制。服務器運行的是什麼版本? – silkfire 2013-02-26 12:34:55

回答

0

看着你HTTP_CODE響應,257是創造了「PATHNAME」的代碼,這可能意味着該文件不存在,或者您的路徑可能是不正確。

使用您知道存在的其他網址進行測試。

+0

thx爲您的答案。我檢查了兩個系統上的URL,它們是正確的。 – DM1986 2013-02-26 12:07:54

0

嘗試切換參數的順序。這是我在我的一個類使用依賴於所在的FTP服務器上的文件:

$fh = fopen('downloads/' . self::ZIP, 'w'); 
$ch = curl_init(self::FTP . self::PATH . '/' . self::ZIP); 

curl_setopt($ch, CURLOPT_USERPWD, self::USERNAME . ':' . self::PASSWORD); 
curl_setopt($ch, CURLOPT_FILE, $fh); 

curl_exec($ch); 
curl_close($ch); 
fclose($fh); 
+0

thx爲您的答案。我試過這樣,但結果是一樣的 – DM1986 2013-02-26 12:07:18