Downloading a file via ftp using php

19,033

Solution 1

Use ftp_get not 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

Solution 2

This is my code, $source_file="http://example.com/ex/ex2/".$file_name; and $destination_file is equal to a path.

     $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);*/
Share:
19,033
guitarlass
Author by

guitarlass

Updated on June 04, 2022

Comments

  • guitarlass
    guitarlass almost 2 years

    I'm using ftp_put() to download a file from another server. Everything (connection etc.) working but it wont download.

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

    above is my code and its giving error "Can't change directory to"... when i out ftp_put without 'ftp_chdir' it didn't work so i used ftp_chdir. but still not working. $destination_file equal to a path and $name equal to a file. Please give me an idea what a i doing wrong? p.s even $upload is returning true. But i can't find the file anywhere.

  • guitarlass
    guitarlass about 12 years
    guess a bit misunderstanding thanks . but now i get this error. failed to open stream: No such file or directory and Error opening . :| do u knw why??
  • guitarlass
    guitarlass about 12 years
    seems like another clumsy miss. But now get this error ftp_get() [function.ftp-get]: Can't open http://example.com/folderx/foldery/test.zip: No such file or directory :(
  • Songo
    Songo about 12 years
    @guitarlass Could you please add the complete code you use for connection and downloading to your question so that I can debug it?
  • guitarlass
    guitarlass about 12 years
    I'll post it in a reply because there is not enough space here. Sorry for late reply. hope you can solve this quickly.
  • Songo
    Songo about 12 years
    what is the value of $ftp_server ?
  • Songo
    Songo about 12 years
    and btw add the code to your question not is a separate answer so that everyone can view it
  • guitarlass
    guitarlass about 12 years
    $ftp_server is equal to something like "example.xy.zo" . that's the host name i put in my file upload/download client.
  • Songo
    Songo about 12 years
    then your $source_file should be something like "/ex/ex2/".$file_name;. It's the absolute path without the host.
  • guitarlass
    guitarlass about 12 years
    ah! i didn't know that. I'll check it and let you know. thanks a loads!!
  • Leander
    Leander almost 11 years
    What do I put in the variable $ftp_server? The PHP documentation is always very incomplete, its hardly usefull.