PHP ftp_rawlist() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
FTP

Definition

In PHP, ftp_rawlist() returns a detailed list of files in a remote folder. Each item is one line of text, just like the output of the ls -l command.

Unlike ftp_nlist(), which only gives names, ftp_rawlist() includes permissions, owner, size and date. You need to split each line yourself if you want those parts separately.

Syntax

Call ftp_rawlist() with the values you need. Start with this line.

PHP
$lines = ftp_rawlist($ftp, '.');
CallWhat it does
ftp_rawlist($ftp, '.')Lists the current folder with full details for each file.
ftp_rawlist($ftp, '/public_html')Lists a specific remote folder.
ftp_rawlist($ftp, '.', true)Lists the folder and all its sub folders, if the server supports it.

The format of each line depends on the server. Most Unix servers return lines like -rw-r--r-- 1 user group 1024 Jan 10 12:00 file.txt, but Windows servers may use a different style.

Library declarations
PHP
ftp_rawlist(FTP\Connection $ftp, string $directory, bool $recursive = false): array|false

Parameters

ParameterDescription
ftpRequired. The FTP connection returned by ftp_connect() or ftp_ssl_connect(). You should already be logged in.
directoryRequired. The remote directory to list. Use . for the current folder.
recursiveOptional. If true, the command is run as LIST -R and sub folders are included. The default is false.

Return Value

Return ValueDescription
array|falseReturns an array where each item is a line of the listing, or false if the listing failed.

PHP Examples

Example 1 — List the Current Folder

Print the detailed list of files in the current remote directory.

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

$lines = ftp_rawlist($ftp, '.');

foreach ($lines as $line) {
    echo $line, PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. ftp_pasv() turns on passive mode so the listing works through most firewalls.
  2. ftp_rawlist() asks the server for a detailed list of the current folder.
  3. Each array item is one line, similar to the ls -l command.
  4. foreach prints every line so you can see permissions, size, date and name.

Example 2 — Handle a Failed Listing

ftp_rawlist() returns false when the folder does not exist or cannot be read.

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

$lines = @ftp_rawlist($ftp, '/no-such-folder');

if ($lines === false) {
    echo "Could not list the folder", PHP_EOL;
} elseif (count($lines) === 0) {
    echo "The folder is empty", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. The @ symbol hides the PHP warning so we can print our own message.
  2. A missing folder makes ftp_rawlist() return false.
  3. An empty but valid folder returns an empty array instead, so the second check handles that case.
  4. Always test for false using === so these two results are not confused.

Example 3 — Pick Out Names and Sizes

Split each Unix style line into parts to show the file name and size.

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

foreach (ftp_rawlist($ftp, '.') as $line) {
    $parts = preg_split('/\s+/', $line, 9);
    $type  = $parts[0][0] === 'd' ? 'folder' : 'file';
    echo "{$parts[8]} ({$type}, {$parts[4]} bytes)", PHP_EOL;
}

ftp_close($ftp);

How It Works

  1. preg_split() cuts each line at the spaces into at most nine parts.
  2. The first character of the permissions tells us if it is a folder (d) or a file.
  3. Part number 4 is the size and part number 8 is the file name.
  4. This only works for Unix style listings, so test it on your own server first.

Frequently Asked Questions

It returns a detailed listing of a remote folder as an array of text lines, including permissions, owner, size, date and file name.
ftp_nlist() returns only file names, while ftp_rawlist() returns full details in one line per file. Use ftp_rawlist() when you need size or date.
The format is chosen by the server. Unix and Windows FTP servers print different styles, so parsing code may need changes.
Pass true as the third argument. The server then runs a recursive listing, but not every server supports this option.
Yes. ftp_mlsd() returns structured arrays with keys like type, size and modify, when the server supports the MLSD command.

Next: ftp_rename()

ftp_rename() renames or moves a file or folder on a remote FTP server.

ftp_rename() 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