What are the potential security risks of using FTP for file downloads in PHP scripts?
Using FTP for file downloads in PHP scripts can pose security risks such as exposing credentials in plain text, potential for FTP server vulnerabilities, and lack of encryption for data transfer. To mitigate these risks, it is recommended to use secure protocols like SFTP or HTTPS for file downloads instead.
// Example of using SFTP for file download in PHP
$ftp_server = 'sftp.example.com';
$ftp_username = 'username';
$ftp_password = 'password';
$connection = ssh2_connect($ftp_server);
ssh2_auth_password($connection, $ftp_username, $ftp_password);
$sftp = ssh2_sftp($connection);
$remote_file = 'example.txt';
$local_file = '/path/to/local/example.txt';
if (ssh2_scp_recv($connection, $remote_file, $local_file)) {
echo 'File downloaded successfully.';
} else {
echo 'Failed to download file.';
}
Related Questions
- What is the best way to store and update date and time values in a PHP form to a database?
- What are the common pitfalls when trying to perform conditional checks on values in PHP and MySQL?
- Are there any best practices for handling input fields and form submissions in PHP to prevent data discrepancies?