What are the best practices for establishing and logging into an FTP server using PHP?

Establishing and logging into an FTP server using PHP involves using the FTP functions provided by PHP to connect to the server, authenticate with a username and password, and perform file operations. It is important to securely handle the credentials and close the FTP connection after use to prevent security vulnerabilities.

// FTP server credentials
$ftp_server = 'ftp.example.com';
$ftp_user = 'username';
$ftp_pass = 'password';

// Connect to FTP server
$conn_id = ftp_connect($ftp_server);

// Login to FTP server
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);

// Check if login was successful
if ($login_result) {
    echo 'Connected and logged in to FTP server.';
} else {
    echo 'Failed to connect and login to FTP server.';
}

// Close FTP connection
ftp_close($conn_id);