PHP Function ftp_nlist - List the Contents of a Directory on an FTP Server
php function ftp_nlist is one of the functions that allows you to list the contents of a directory on an FTP server. The other main FTP functions are ftp_get() and ftp_put() which allow you to download and upload files.
Before you can list the files on an ftp server it is necessary to connect to and login to the ftp server. To do this you can use the ftp_connect() function which takes a host name, port number and optionally a timeout limit. Once connected you can access the directories and files on the server.
There will be times when you need to retrieve a list of new files from an ftp server. This can be useful if you are renaming or deleting files on the server and need to know which file has been modified since the last time you looked. There are a few ways to do this in PHP but they all involve using the ftp_nlist() function.
The ftp_nlist() function requires two parameters, a ftp_stream and the directory path that you want to list. The ftp_stream is the connection identifier returned by the ftp_connect() function and the directory path is the path on the FTP server to which you are connecting. The return value is an array of filenames if successful or false otherwise.
Phil is a senior web developer at Digital Ocean with over 12 years of IT experience. He has worked as a database administrator, on an IT help desk, systems trainer and as a web architect. He is passionate about helping people learn and has written numerous guides, books and articles on PHP. He is also an avid photographer, musician and enjoys travelling.