Current Location: Home> Latest Articles> Does ftp_nlist return a list of hidden files? How to determine?

Does ftp_nlist return a list of hidden files? How to determine?

M66 2025-06-14

1. ftp_nlist Returns a List of Hidden Files?

First, it is important to understand how ftp_nlist works. This function requests a list of filenames from a specified directory via the FTP protocol and returns the results as an array. As for hidden files, ftp_nlist does not automatically exclude files that begin with a dot (.). That means hidden files will appear in the returned file list unless the FTP server configuration or the specific directory does not contain them.

You can verify this by manually checking the returned file list on the server. In many UNIX-based systems, hidden files typically begin with a dot (.), such as .gitignore or .bashrc.

$ftp_connection = ftp_connect("ftp.m66.net");
ftp_login($ftp_connection, "username", "password");
<p>$file_list = ftp_nlist($ftp_connection, "/path/to/directory");</p>
<p>print_r($file_list);<br>

The above code returns the $file_list array. If the directory contains files that start with a dot, they will be listed.

2. How to Determine if a File is Hidden?

Since ftp_nlist may return hidden files, we need to determine whether a file is hidden. The most straightforward way is to check if the filename starts with a dot (.).

foreach ($file_list as $file) {
    if (strpos($file, '.') === 0) {
        echo $file . " is a hidden file\n";
    } else {
        echo $file . " is not a hidden file\n";
    }
}

The code above checks if the first character of the filename is a dot to determine if it is a hidden file. If the filename begins with a dot, it is considered a hidden file.

3. How to Exclude Hidden Files from the List?

If you wish to exclude hidden files, you can process the file list after retrieving it, keeping only those items that are not hidden files. For example:

$visible_files = array_filter($file_list, function ($file) {
    return strpos($file, '.') !== 0;  // Exclude files starting with a dot
});
<p>print_r($visible_files);<br>

This way, you can ensure that the returned file list does not contain any hidden files, which is suitable for scenarios where you need to display a list of public files.

4. Summary

ftp_nlist by default returns a file list that includes hidden files, especially in UNIX and UNIX-like systems where hidden files begin with a dot. To determine if a file is hidden, you can check if the filename starts with a dot. If you want to filter out hidden files, you can use the array_filter function to keep only those items that are not hidden.