Open In App

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:  

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



Note:  

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:  




<?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 downloaded
        $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);
    // Closing  connection
  
    if(ftp_close($ftp_connection)) {
        echo "<br>Connection closed Successfully!";
    }
}
?>

Output:  

Example 2: Downloading of binary file from FTP Server. 




<?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 downloaded
        $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);
    // Closing  connection
   
    if(ftp_close($ftp_connection)) {
        echo "<br>Connection closed Successfully!";
    }
}
?>

Output: 

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


Article Tags :