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);
Keywords
Related Questions
- How can a PHP developer effectively decide on the best approach to debugging by following established practices or exploring alternative methods?
- What are the advantages and disadvantages of using dynamic JavaScript for visitor counters compared to static image counters?
- What are some best practices for reading data from a MySQL table using PHP?