PHP | ftp_get() function

The ftp_get() function is an inbuilt function in PHP which is used to get or download files from FTP server to local server or machine.

Syntax:

ftp_get( $ftp_connection, $local_file_path, $server_file_path,
                             $mode_of_file_transfer, $starting_position );

Parameters: This function accepts five parameters as mentioned above and described below:



  • $ftp_connection: It is required parameter. It specifies the already existing FTP connection to use for downloading the file from the FTP server.
  • $local_file_path: It is required parameter. It specifies the path of local server or machine to which the file is being downloaded.
  • $server_file_path: It is required parameter. It specifies path of the file is to be downloaded from the FTP server.
  • $mode_of_file_transfer: It is required parameter. It specifies the transfer mode. Values of the parameter is either FTP_ASCII or FTP_BINARY.
  • $starting_position: It is an optional parameter. It specifies the position in the remote file to start downloading.

Return Value: It returns True on success or False on failure.

Note:

  • This function is available for PHP 4.0.0 and newer version.
  • The following examples cannot be run on online IDE. So try to run in some PHP hosting server or localhost with proper ftp server name.
  • Mode should be chosen correctly i.e. either FTP_ASCII or FTP_BINARY

Vital information to run the following code: The following code will not work in online IDE as they don’t allow to interact with files. So try to run on PHP hosting server. Make sure provide correct FTP server, username, password, server file path, and local file path. If the file specifies as server file doesn’t exist then an error will occur so make sure server file exists. In these examples, the file mentioned as $server_file will be downloaded as local file to the relative path as provided in $local_file.

Below examples illustrate the ftp_get() function in PHP:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Connect to FTP server
  
// Use a correct ftp server
$ftp_server = "localhost";
  
// Use correct ftp username
$ftp_username="user";
  
// Use correct ftp password corresponding
// to the ftp username
$ftp_userpass="user";
   
// Establishing ftp connection 
$ftp_connection = ftp_connect($ftp_server
    or die("Could not connect to $ftp_server");
  
if( $ftp_connection ) {
    echo "successfully connected to the ftp server!";
      
    // Logging in to established connection
    // with ftp username password
    $login = ftp_login($ftp_connection,
            $ftp_username, $ftp_userpass);
      
    if($login) {
          
        // Checking whether logged in successfully
        // or not
        echo "<br>logged in successfully!";
          
        // Name or path of the localfile to
        // where the file to be downloaded
        $local_file = "local_file.txt";
          
        // Name or path of the server file to
        // be downoaded
        $server_file = "server_file.txt";
          
        // Downloading the specified server file
        if (ftp_get($ftp_connection, $local_file,
                $server_file, FTP_BINARY)) {
            echo "<br>Successfully downloaded "
               . "from $server_file to $local_file.";
          }
          else {
            echo "<br>Error while downloading from "
                  . "$server_file to $local_file.";
          }
           
    }
    else {
        echo "<br>login failed!";
    }
      
    // echo ftp_get_option($ftp_connection, 1);
    // Closeing  connection
   
    if(ftp_close($ftp_connection)) {
        echo "<br>Connection closed Successfully!";
    
}
?>

chevron_right


Output:

Example 2: Downloading of binary file from FTP Server.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Connect to FTP server
   
// Use a correct ftp server
$ftp_server = "localhost";
   
// Use correct ftp username
$ftp_username="user";
   
// Use correct ftp password corresponding
// to the ftp username
$ftp_userpass="user";
    
// Establishing ftp connection 
$ftp_connection = ftp_connect($ftp_server, 21) 
    or die("Could not connect to $ftp_server");
   
if( $ftp_connection ) {
    echo "successfully connected to the ftp server!";
       
    // Logging in to established connection
    // with ftp username password
    $login = ftp_login($ftp_connection,
            $ftp_username, $ftp_userpass);
       
    if($login) {
           
        // Checking whether logged in successfully
        // or not
        echo "<br>logged in successfully!";
           
        // Name or path of the localfile to
        // where the file to be downloaded
        $local_file = "local_file_shiva.jpg";
           
        // Name or path of the server file to
        // be downoaded
        $server_file = "shiva.jpg";
           
        // Downloading the specified server file
        if (ftp_get($ftp_connection, $local_file,
                $server_file, FTP_BINARY)) {
            echo "<br>Successfully downloaded from"
                . " $server_file to $local_file.";
          }
          else {
            echo "<br>Error while downloading from"
                . " $server_file to $local_file.";
          }
            
    }
    else {
        echo "<br>login failed!";
    }
       
    // echo ftp_get_option($ftp_connection, 1);
    // Closeing  connection
    
    if(ftp_close($ftp_connection)) {
        echo "<br>Connection closed Successfully!";
    
}
?>

chevron_right


Output:

Reference: https://www.php.net/manual/en/function.ftp-get.php



My Personal Notes arrow_drop_up

I am unrecognized crazy being with random thoughts

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.