2012-04-01 183 views
1

我正在使用ftp_put()從另一臺服務器下載文件。一切(連接等)工作,但它不會下載。上述使用php下載文件通過ftp

ftp_chdir($conn_id, $destination_file); 
    $upload = ftp_put($conn_id, $name, $source_file, FTP_BINARY); 

是我的代碼和它給錯誤「無法更改目錄」 ...... 當我走出ftp_put沒有「ftp_chdir」它沒有,所以我使用ftp_chdir工作。但仍然無法正常工作。 $destination_file等於一個路徑,而$name等於一個文件。請給我一個想法,我做錯了什麼? p.s even $ upload返回true。但我無法在任何地方找到該文件。

+2

FTP '放' 是上傳... – 2012-04-01 13:40:00

+1

http://php.net/ftp_put VS http://php.net/ftp_get – mishu 2012-04-01 13:41:09

回答

4

使用ftp_getftp_put

<?php 

// define some variables 
$local_file = 'local.zip'; 
$server_file = 'server.zip'; 

// set up basic connection 
$conn_id = ftp_connect($ftp_server); 

// login with username and password 
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

// try to download $server_file and save to $local_file 
if (ftp_get($conn_id, $local_file, $server_file, FTP_BINARY)) { 
    echo "Successfully written to $local_file\n"; 
} else { 
    echo "There was a problem\n"; 
} 

// close the connection 
ftp_close($conn_id); 

?> 

ftp_get from PHP manual

+0

猜測有點誤會感謝。但現在我得到這個錯誤。 '無法打開流:沒有這樣的文件或目錄'和'錯誤打開'。 :|你呃爲什麼? – guitarlass 2012-04-02 05:15:55

+0

看起來像另一個笨拙的小姐。但現在得到這個錯誤'ftp_get()[function.ftp-get]:無法打開http://example.com/folderx/foldery/test.zip:沒有這樣的文件或目錄':( – guitarlass 2012-04-02 06:08:25

+0

@guitarlass能不能請將您用於連接和下載的完整代碼添加到您的問題中,以便我可以調試它? – Songo 2012-04-03 17:09:59

1

這是我的代碼, $source_file="http://example.com/ex/ex2/".$file_name;$destination_file等於路徑。

 $conn_id = ftp_connect($ftp_server); 

    // login with username and password 
    $login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

    // check connection 
    if ((!$conn_id) || (!$login_result)) { 
      echo "FTP connection has failed!"; 
      echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
      exit; 
     } else { 
      echo "Connected to $ftp_server, for user $ftp_user_name"; 
     } 

    // upload the file 
    //ftp_chdir($conn_id, $destination_file); 
    $upload = ftp_get($conn_id, $destination_file, $source_file, FTP_BINARY); 

    // check upload status 
    if (!$upload) { 
      echo "FTP upload has failed!"; 
     } else { 
      echo "Downloaded $source_file"; 
     } 

    // close the FTP stream 
    ftp_close($conn_id);*/ 
+1

$ ftp_server的值是什麼? – Songo 2012-04-04 11:13:19

+0

和btw將代碼添加到您的問題不是一個單獨的答案,以便每個人可以查看 – Songo 2012-04-04 11:15:07

+0

'$ ftp_server'等於「example.xy.zo」,這是我放入我的文件上傳/下載客戶端的主機名 – guitarlass 2012-04-05 10:49:04