What are the security implications of using FTP in conjunction with PHP for file uploads, and how can developers mitigate any potential risks?
When using FTP in conjunction with PHP for file uploads, there is a risk of exposing sensitive information such as FTP credentials. To mitigate this risk, developers should avoid hardcoding FTP credentials in their PHP code and instead use environment variables or a secure configuration file.
// Load FTP credentials from a secure configuration file
$config = parse_ini_file('config.ini');
$ftp_server = $config['ftp_server'];
$ftp_username = $config['ftp_username'];
$ftp_password = $config['ftp_password'];
// Connect to FTP server
$conn_id = ftp_connect($ftp_server);
ftp_login($conn_id, $ftp_username, $ftp_password);
// Upload file to FTP server
ftp_put($conn_id, 'remote/path/file.txt', 'local/path/file.txt', FTP_ASCII);
// Close FTP connection
ftp_close($conn_id);