使用 php 通过 ftp 下载文件


Downloading a file via ftp using php

我正在使用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等于文件。请告诉我我做错了什么?附言即使$upload也回归真实。但是我在任何地方都找不到该文件。

使用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 来自 PHP 手册

这是我

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