FTP connection: retrieve files

1. Authorization data and FTP server info:

ini_set("max_execution_time", "0");
ini_set("memory_limit", "-1");

$ftp_server = "ftp.server.com";
$ftp_folder = "/folder_with_files/";
$ftp_user_name = "user_name_as_login";
$ftp_user_pass = "user_password";

// optional
$ftp_transferred = 0;

2. Then make a connection and be sure that no errors appears:

$conn_id = ftp_connect($ftp_server) or die("Cannot establish FTP connection to $ftp_server");
$login   = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die("Loginization has been failed.");
$mode    = ftp_pasv($conn_id, TRUE) or die("Cannot turn on passive mode.");  // It is optional, when ftp_nlist/ftp_rawlist/ftp_mlsd returns FALSE

3. Now you need enter into the chosen dir:

ftp_chdir($conn_id, $ftp_folder);
echo "Current directory: " . ftp_pwd($conn_id) . "<br>";

4. … and now you can look at files and dirs inside the chosen folder:

$file_list = [];
$files_count = 0;
if ($file_list = ftp_nlist($conn_id, "")) {
     $files_count = count($file_list);
};

echo "Amount of files in that folder is: " . $files_count . "<br>";

5. Each file could be stored in any dir on your webserver. Don’t forget to close FTP connection:

$path = './files/defined_folder/';

foreach ($file_list as $file) {

    // echo "<li>" . $file . "</li><br>";

    if (ftp_get($conn_id, $pth . $file, $file, FTP_BINARY)) {
       $ftp_transferred++;
       // echo "Successfully written to $path";
    } else {
        echo "There was a problem";
    }
}

ftp_close($conn_id);
echo 'Transferred from FTP ' . $this->ftp_transferred . ' files for ' . ((microtime(true) - $time_start) / 60) . ' minutes.';

Note: $path directory MUST has a permission to write files in it (website often named as apache ).

class Sandbox 
{
    const SOME_LOCAL_FOLDER = 'forecasts/'; 
 
    private $ftp_transferred = 0;
 
public function lookAtFTPfolder()
    {
        $time_start = microtime(true);

        ini_set("max_execution_time", "0");
        ini_set("memory_limit", "-1");
 
        $ftp_server = "ftp.server.com";
        $ftp_folder = "/folder_with_files/";
        $ftp_user_name = "user_name_as_login";
        $ftp_user_pass = "user_password";
 
        $conn_id = ftp_connect($ftp_server) or die("Cannot establish FTP connection to $ftp_server");
        $login   = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass) or die("Loginization has been failed.");
        $mode    = ftp_pasv($conn_id, TRUE) or die("Cannot turn on passive mode.");

        if (ftp_chdir($conn_id, $ftp_folder)) {

            echo "Current directory: " . ftp_pwd($conn_id) . "<br>";

            $file_list = [];
            $files_count = 0;
            if ($file_list = ftp_nlist($conn_id, "")) {
                $files_count = count($file_list);
            };

            echo "Amount of files in that folder is: " . $files_count . "<br>";

            $pth = './files/' . self::SOME_LOCAL_FOLDER;

            foreach ($file_list as $file) {

                // echo "<li>" . $file . "</li><br>";

                if (ftp_get($conn_id, $pth . $file, $file, FTP_BINARY)) {

                    $this->ftp_transferred++;
                    // echo "Successfully written to $path";

                    // optional. Delete the copied file from FTP server
                    // ftp_delete($conn_id, $file);
                } else {
                    echo "There was a problem";
                }
            }

            ftp_close($conn_id);
        } else {
            die("An access to chosen folder is denied.");
        }
        echo 'Transferred from FTP ' . $this->ftp_transferred . ' files for ' . ((microtime(true) - $time_start) / 60) . ' minutes.';
    }
}