What are common pitfalls when using FTP functions in PHP?

Common pitfalls when using FTP functions in PHP include not handling errors properly, not setting the correct permissions for files and directories, and not securely storing FTP credentials. To avoid these pitfalls, always check for errors when using FTP functions, ensure that files and directories have the correct permissions, and securely store FTP credentials in a separate configuration file.

// Example of securely storing FTP credentials in a separate configuration file
$config = parse_ini_file('config.ini');
$ftp_server = $config['ftp_server'];
$ftp_username = $config['ftp_username'];
$ftp_password = $config['ftp_password'];

// Example of handling errors when using FTP functions
$conn_id = ftp_connect($ftp_server);
if (!$conn_id) {
    die("Failed to connect to FTP server");
}

$login_result = ftp_login($conn_id, $ftp_username, $ftp_password);
if (!$login_result) {
    die("Failed to login to FTP server");
}

// Example of setting correct permissions for files and directories
ftp_chmod($conn_id, 0644, 'example.txt');
ftp_chmod($conn_id, 0755, 'example_directory');