What are the potential pitfalls of using FTP for file management in PHP?

Potential pitfalls of using FTP for file management in PHP include security vulnerabilities, such as exposing login credentials in plain text, and potential for unauthorized access if the FTP server is not properly secured. To mitigate these risks, it is recommended to use secure protocols like SFTP or FTPS, and ensure that proper authentication and authorization mechanisms are in place.

// Example of using SFTP for file management in PHP
$connection = ssh2_connect('sftp.example.com', 22);
ssh2_auth_password($connection, 'username', 'password');

$sftp = ssh2_sftp($connection);
$stream = fopen("ssh2.sftp://$sftp/path/to/file.txt", 'r');

while (!feof($stream)) {
    echo fread($stream, 8192);
}

fclose($stream);