What are common pitfalls when uploading files via FTP in PHP?
Common pitfalls when uploading files via FTP in PHP include not setting the correct permissions on the destination directory, not handling errors properly, and not sanitizing user input to prevent security vulnerabilities.
// Example PHP code snippet to upload a file via FTP with error handling and proper permissions
$ftp_server = "ftp.example.com";
$ftp_username = "username";
$ftp_password = "password";
$local_file = "local_file.txt";
$remote_file = "remote_file.txt";
$ftp_conn = ftp_connect($ftp_server);
$login = ftp_login($ftp_conn, $ftp_username, $ftp_password);
if (!$ftp_conn || !$login) {
die("FTP connection failed");
}
if (!ftp_put($ftp_conn, $remote_file, $local_file, FTP_ASCII)) {
die("Failed to upload file");
}
ftp_chmod($ftp_conn, 0644, $remote_file);
ftp_close($ftp_conn);