2012-04-01 178 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