What are the potential security risks involved in creating and uploading files to an FTP server using PHP?
One potential security risk is that the FTP login credentials are hardcoded in the PHP code, making them vulnerable to exposure. To mitigate this risk, it is recommended to store the credentials in a secure location outside of the web root directory and access them securely within the PHP script.
<?php
// Load FTP credentials from a secure location
include '/path/to/secure/ftp_credentials.php';
// Connect to FTP server using credentials
$ftp_server = "ftp.example.com";
$ftp_user = $ftp_credentials['username'];
$ftp_pass = $ftp_credentials['password'];
$ftp_conn = ftp_connect($ftp_server);
$login = ftp_login($ftp_conn, $ftp_user, $ftp_pass);
// Upload file to FTP server
$file_path = "/path/to/local/file.txt";
$remote_file = "file.txt";
if (ftp_put($ftp_conn, $remote_file, $file_path, FTP_ASCII)) {
echo "File uploaded successfully";
} else {
echo "Error uploading file";
}
// Close FTP connection
ftp_close($ftp_conn);
?>