In PHP, ftp_nlist() returns a simple list of file and folder names inside a directory on an FTP server.
It is the quickest way to see what is on the server when you only need names. If you also want sizes, types or dates, use ftp_mlsd() or ftp_rawlist() instead.
Syntax
Call ftp_nlist() with the values you need. Start with this line.
PHP
$files = ftp_nlist($ftp, '.');
Call
What it does
ftp_nlist($ftp, '.')
Returns the names in the current remote directory.
ftp_nlist($ftp, '/public_html')
Lists a specific remote directory.
ftp_nlist($ftp, '/public_html/*.php')
Some servers let you add a pattern, though it is not guaranteed.
Depending on the server, each name may include the directory path, for example public_html/index.php. Use basename() if you only want the last part. Turn on passive mode with ftp_pasv() if the list comes back empty or false.
The ?: [] part gives you an empty array if ftp_nlist() returns false.
array_map('basename', . ..) strips any folder path from every name.
array_filter() keeps only names that end with . php.
array_values() resets the array keys so the output starts at 0.
Frequently Asked Questions
It returns an array with the names of the files and folders in a remote FTP directory.
False means an error, such as a missing directory or a blocked data connection. An empty array means the directory is empty. Try ftp_pasv($ftp, true) if you suspect a firewall.
No, only names. Use ftp_size() for one file, or ftp_mlsd() and ftp_rawlist() to get sizes and dates in a single call.
Some servers return the full relative path. Use basename() to keep only the file name.
ftp_nlist() returns only names, while ftp_rawlist() returns detailed text lines in the format of the server's ls -l command.