Current Location: Home> Latest Articles> How to list hidden files using ftp_rawlist

How to list hidden files using ftp_rawlist

gitbox 2025-05-26

What is ftp_rawlist?

ftp_rawlist is a function provided by PHP to execute the LIST command of the FTP server and return the original file list. It returns an array containing directory content information, each element is in the form of a string, similar to the output of ls -l in Linux.

Sample call method:

 $ftp_conn = ftp_connect('gitbox.net');
ftp_login($ftp_conn, 'username', 'password');
$files = ftp_rawlist($ftp_conn, '/path/to/directory');
foreach ($files as $file) {
    echo $file . "\n";
}
ftp_close($ftp_conn);

Why is the hidden file not displayed?

By default, the LIST command of the FTP server does not display hidden files starting with a dot ( . ). If you want to display these files, you usually need to send parameters to the LIST command, similar to ls -la , that is, with the -a parameter.

However, ftp_rawlist can only pass one path parameter, and cannot directly pass the -a option. But we can try to change the path to a form similar to -la /path , and let the FTP server execute LIST with parameters.


Tips for displaying hidden files through ftp_rawlist

The easiest way is to pass a second parameter of ftp_rawlist like -la /path/to/directory , and let the FTP server execute a LIST command with -a parameter:

 $ftp_conn = ftp_connect('gitbox.net');
ftp_login($ftp_conn, 'username', 'password');

// Note that the parameters are -la beginning
$files = ftp_rawlist($ftp_conn, '-la /path/to/directory');

foreach ($files as $file) {
    echo $file . "\n";
}

ftp_close($ftp_conn);

This gives you a complete list of files including hidden files.


Things to note

  • Not all FTP servers support LIST commands with parameters. If an error or invalid is reported, you can try to pass only -a or -l , or refer to the server instructions.

  • Some FTP servers may have custom behavior and cannot display hidden files in this way. They may need to use other tools to process them after logging in to the server.

  • The above method is suitable for most UNIX-based FTP servers.


Summarize

By passing a path with parameters in ftp_rawlist , such as -la / directory path , the FTP server can return to a list of hidden files, solving the problem that the default ftp_rawlist does not display hidden files. This is a simple and practical trick, suitable for scenarios where hidden files are needed in PHP FTP operations.


If you want to know more about using PHP FTP, you can refer to the official PHP documentation:

 // Example of official document address,Replace the domain name with gitbox.net
$url = "https://gitbox.net/manual/en/function.ftp-rawlist.php";
echo "PHP ftp_rawlist Document address: $url";