What function can be used to check if a file exists on an FTP server in PHP?

To check if a file exists on an FTP server in PHP, you can use the ftp_size() function. This function returns the size of the specified file if it exists, or false if the file does not exist. By checking the return value of ftp_size(), you can determine if the file exists on the FTP server.

$ftp_server = 'ftp.example.com';
$ftp_user = 'username';
$ftp_pass = 'password';
$file_path = '/path/to/file.txt';

$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);

if (ftp_size($conn_id, $file_path) !== -1) {
    echo 'File exists on FTP server';
} else {
    echo 'File does not exist on FTP server';
}

ftp_close($conn_id);