What potential pitfalls should be considered when using PHP to display all files in an FTP folder?

One potential pitfall when using PHP to display all files in an FTP folder is that it can expose sensitive information if not properly secured. To solve this issue, you should ensure that the FTP connection is secure and that you have proper authentication in place to restrict access to unauthorized users.

<?php
$ftp_server = "ftp.example.com";
$ftp_user = "username";
$ftp_pass = "password";

// establish connection
$conn_id = ftp_connect($ftp_server);
$login_result = ftp_login($conn_id, $ftp_user, $ftp_pass);

// check if connection is successful
if ($conn_id && $login_result) {
    // get list of files in FTP folder
    $files = ftp_nlist($conn_id, ".");
    
    // output list of files
    foreach ($files as $file) {
        echo $file . "<br>";
    }
    
    // close connection
    ftp_close($conn_id);
} else {
    echo "Failed to connect to FTP server";
}
?>