What FTP commands can be used to check if a directory exists in PHP?
To check if a directory exists on an FTP server using PHP, you can use the FTP command "NLST" (Name List). This command will return a list of filenames in a specified directory. You can then check if the directory you are looking for is present in the list.
<?php
$ftp_server = 'ftp.example.com';
$ftp_user = 'username';
$ftp_pass = 'password';
$ftp_dir = '/path/to/directory';
$ftp_conn = ftp_connect($ftp_server);
$login = ftp_login($ftp_conn, $ftp_user, $ftp_pass);
if ($login) {
$file_list = ftp_nlist($ftp_conn, $ftp_dir);
if (in_array($ftp_dir, $file_list)) {
echo "Directory exists";
} else {
echo "Directory does not exist";
}
ftp_close($ftp_conn);
} else {
echo "FTP login failed";
}
?>
Keywords
Related Questions
- Are there any best practices or guidelines to follow when creating and reading directories in PHP to avoid errors like the one mentioned in the thread?
- What are some common pitfalls when using str_replace() in PHP?
- What are the differences in handling Content-Type headers for different browsers when serving files with PHP, especially in the case of Internet Explorer?