PHP ftp_mlsd() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

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, '.');
CallWhat 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).

Library declarations
PHP
ftp_mlsd(FTP\Connection $ftp, string $directory): array|false

Parameters

ParameterDescription
ftpRequired. The FTP\Connection object returned by ftp_connect() or ftp_ssl_connect().
directoryRequired. The remote directory to list. Use "." for the current directory.

Return Value

Return ValueDescription
arrayA list of arrays, one per entry, each holding the facts the server reported.
falseReturned on failure, for example when the server does not support MLSD.

PHP Examples

Example 1 — List Names and Types

Print the name and type of every entry in the current directory.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'username', 'password');

$items = ftp_mlsd($ftp, '.');

foreach ($items as $item) {
    echo $item['type'], ': ', $item['name'], PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. ftp_mlsd() returns an array where every entry is its own array of facts.
  2. foreach goes through each entry one at a time.
  3. $item['type'] tells you if the entry is a file, dir, cdir or pdir.
  4. $item['name'] holds the file or folder name.

Example 2 — Fall Back When MLSD Is Not Supported

If the server does not support MLSD, ftp_mlsd() returns false. Use ftp_nlist() instead.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'username', 'password');
ftp_pasv($ftp, true);

$items = @ftp_mlsd($ftp, '.');

if ($items === false) {
    echo "MLSD not supported, using ftp_nlist()", PHP_EOL;
    $names = ftp_nlist($ftp, '.');
} else {
    $names = array_column($items, 'name');
}

print_r($names);

ftp_close($ftp);

How It Works

  1. Passive mode is turned on, which helps listings work behind a firewall.
  2. @ftp_mlsd() tries the structured listing and returns false on unsupported servers.
  3. The fallback branch calls ftp_nlist() to get plain names.
  4. Either way, $names ends up as a simple list of names.

Example 3 — Show Only Files With Their Sizes

Filter the result by type and print each file size using the facts that MLSD gives you.

PHP
<?php
$ftp = ftp_connect('ftp.example.com');
ftp_login($ftp, 'username', 'password');
ftp_pasv($ftp, true);

foreach (ftp_mlsd($ftp, '.') as $item) {
    if ($item['type'] !== 'file') {
        continue;
    }

    $size = (int) $item['size'];
    $date = date('Y-m-d', strtotime($item['modify']));
    echo "{$item['name']} - {$size} bytes - {$date}", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. The loop skips everything that is not a regular file, including cdir, pdir and dir entries.
  2. The size fact is a string, so it is cast to int.
  3. The modify fact looks like 20261110143205, which strtotime() can read.
  4. 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.

Next: ftp_nb_continue()

ftp_nb_continue() continues a non-blocking FTP transfer that you started with ftp_nb_get().

ftp_nb_continue() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful