In PHP, ftp_mlsd() returns a list of the files and folders inside a remote directory. Each item is an array of facts such as name, type, size and modify.
Older functions like ftp_nlist() give only names, and ftp_rawlist() gives text you must parse yourself. ftp_mlsd() gives clean, structured data, but the server must support the MLSD command. It is available from PHP 7.2.
Syntax
Call ftp_mlsd() with the values you need. Start with this line.
PHP
$items = ftp_mlsd($ftp, '.');
Call
What it does
ftp_mlsd($ftp, '.')
Returns an array of facts for every entry in the current remote directory.
ftp_mlsd($ftp, '/public_html')
Lists a specific remote directory.
array_column(ftp_mlsd($ftp, '.'), 'name')
Pulls out just the file names from the result.
Every element is an associative array. Common keys are name, type, size, modify and perm, but the exact facts depend on the server. The type is file, dir, cdir (current directory) or pdir (parent directory).
The loop skips everything that is not a regular file, including cdir, pdir and dir entries.
The size fact is a string, so it is cast to int.
The modify fact looks like 20261110143205, which strtotime() can read.
Name, size and date are printed together on one line.
Frequently Asked Questions
It returns an array of entries for a remote directory. Each entry is an associative array of facts such as name, type, size and modify.
ftp_nlist() returns only names, ftp_rawlist() returns raw text lines you must parse, and ftp_mlsd() returns structured arrays of facts.
The server may not support the MLSD command, the directory may not exist, or passive mode may be needed. Try ftp_pasv() and fall back to ftp_rawlist().
Common values are file, dir, cdir for the current directory and pdir for the parent directory. Some servers add their own values.
ftp_mlsd() was added in PHP 7.2, so it is available in every PHP 8 release.