使用php通过ftp下载文件

时间:2012-04-01 13:35:59

标签: php ftp download

我正在使用ftp_put()从另一台服务器下载文件。一切(连接等)工作,但它不会下载。

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

上面是我的代码及其给出错误“无法将目录更改为”... 当我没有'ftp_chdir'我ftp_put时,它没有用,所以我使用了ftp_chdir。但仍然没有工作。 $destination_file等于路径,$name等于文件。请让我知道我做错了什么? p.s even $ upload返回true。但我无法在任何地方找到该文件。

2 个答案:

答案 0 :(得分:4)

使用ftp_get而不是ftp_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

答案 1 :(得分:2)

这是我的代码, $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);*/