How can PHP be used to interact with FTP directories and files?

To interact with FTP directories and files using PHP, you can use the built-in FTP functions provided by PHP. These functions allow you to connect to an FTP server, navigate directories, upload/download files, and perform other FTP operations.

// Connect to FTP server
$ftp_server = 'ftp.example.com';
$ftp_user = 'username';
$ftp_pass = 'password';
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);

// List files in directory
$files = ftp_nlist($conn_id, '/path/to/directory');
foreach ($files as $file) {
    echo $file . "<br>";
}

// Upload a file
$local_file = 'local_file.txt';
$remote_file = 'remote_file.txt';
ftp_put($conn_id, $remote_file, $local_file, FTP_ASCII);

// Download a file
$local_file = 'local_file.txt';
$remote_file = 'remote_file.txt';
ftp_get($conn_id, $local_file, $remote_file, FTP_ASCII);

// Close FTP connection
ftp_close($conn_id);