ftp_rawlist() function in PHP

The ftp_rawlist() function displays the list of files with file information from a specified directory on an FTP server. Unlike ftp_nlist(), it returns detailed file information including permissions, size, and modification date.

Syntax

ftp_rawlist(connection, directory, recursive);

Parameters

  • connection − Required. The FTP connection identifier.

  • directory − Required. The directory path to list files from.

  • recursive − Optional. Set to TRUE to enable recursive listing. Default is FALSE.

Return Value

The ftp_rawlist() function returns an array where each element corresponds to one line of text containing detailed file information. Returns FALSE on failure.

Example

The following example demonstrates how to get a detailed list of files from an FTP directory −

<?php
    $ftp_server = "ftp.example.com";
    $ftp_user = "username";
    $ftp_pass = "password";
    
    // Establish connection
    $conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
    $login = ftp_login($conn, $ftp_user, $ftp_pass);
    
    if (!$login) {
        die("FTP login failed");
    }
    
    // Get raw file listing
    $file_list = ftp_rawlist($conn, "/");
    
    if ($file_list !== false) {
        echo "Files and directories:<br>";
        foreach ($file_list as $file) {
            echo $file . "<br>";
        }
    } else {
        echo "Failed to get file listing";
    }
    
    // Close connection
    ftp_close($conn);
?>
Files and directories:
drwxr-xr-x   2 user  group    4096 Jan 15 10:30 documents
-rw-r--r--   1 user  group    1024 Jan 14 14:25 readme.txt
-rw-r--r--   1 user  group    2048 Jan 13 09:15 data.csv

Recursive Listing

You can enable recursive listing to include subdirectories −

<?php
    // Enable recursive listing
    $file_list = ftp_rawlist($conn, "/public", true);
    
    if ($file_list !== false) {
        foreach ($file_list as $file) {
            echo $file . "<br>";
        }
    }
?>

Conclusion

The ftp_rawlist() function provides detailed file information including permissions, ownership, and timestamps. Use it when you need more than just filenames, unlike the simpler ftp_nlist() function.

Updated on: 2026-03-15T07:41:58+05:30

153 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements