What are best practices for securely handling FTP credentials in PHP scripts?

To securely handle FTP credentials in PHP scripts, it is recommended to store the credentials in a separate configuration file outside of the web root directory. This prevents unauthorized access to the credentials through direct URL access. Additionally, consider encrypting the credentials in the configuration file for an added layer of security.

// config.php
define('FTP_HOST', 'ftp.example.com');
define('FTP_USER', 'username');
define('FTP_PASS', 'password123');

// ftp_script.php
include 'config.php';

$ftp_connection = ftp_connect(FTP_HOST);
$login = ftp_login($ftp_connection, FTP_USER, FTP_PASS);

// Perform FTP operations here

ftp_close($ftp_connection);