Current Location: Home> Latest Articles> How to Use the scandir Function to List Hidden Files? Step-by-Step Guide

How to Use the scandir Function to List Hidden Files? Step-by-Step Guide

gitbox 2025-09-02

In PHP, the scandir function is commonly used to list all files and folders in a directory. By default, it does not include hidden files (those starting with a dot .). This can be inconvenient in some cases, such as when you want to list everything in a directory, including hidden files. This article will walk you step by step on how to use the scandir function to list hidden files.

1. Introduction to the scandir Function

The basic syntax of the scandir function is as follows:

<span><span><span class="hljs-variable">$files</span></span><span> = </span><span><span class="hljs-title function_ invoke__">scandir</span></span><span>(</span><span><span class="hljs-variable">$directory</span></span><span>, </span><span><span class="hljs-variable">$sorting_order</span></span><span>);
</span></span>
  • $directory: Specifies the path of the directory to list files from.

  • $sorting_order: Optional. Specifies the sorting order; the default is ascending order.

The function returns an array containing the names of files and folders in the directory. If the directory does not exist or cannot be accessed, it returns false.

2. By Default, scandir Does Not List Hidden Files

If we run the following code:

<span><span><span class="hljs-variable">$files</span></span><span> = </span><span><span class="hljs-title function_ invoke__">scandir</span></span><span>(</span><span><span class="hljs-string">&#039;/path/to/directory&#039;</span></span><span>);
</span><span><span class="hljs-title function_ invoke__">print_r</span></span><span>(</span><span><span class="hljs-variable">$files</span></span><span>);
</span></span>

The output might look like this:

<span><span>Array
(
    [</span><span><span class="hljs-meta">0</span></span><span>] => .
    [</span><span><span class="hljs-meta">1</span></span><span>] => ..
    [</span><span><span class="hljs-meta">2</span></span><span>] => file1.txt
    [</span><span><span class="hljs-meta">3</span></span><span>] => folder1
    [</span><span><span class="hljs-meta">4</span></span><span>] => file2.txt
)
</span></span>

As you can see, hidden files in the directory (such as .env) are not listed. Only the current directory . and parent directory .. are included.

3. How to List Hidden Files?

To make scandir also include hidden files, you can use two approaches: one is a custom filtering method, and the other is filtering the array returned by scandir.

Method 1: Custom Filtering

You can define a custom filter that manually checks if filenames returned by scandir start with a . and decide whether to keep them.

<span><span><span class="hljs-variable">$directory</span></span><span> = </span><span><span class="hljs-string">&#039;/path/to/directory&#039;</span></span><span>;
</span><span><span class="hljs-variable">$files</span></span><span> = </span><span><span class="hljs-title function_ invoke__">scandir</span></span><span>(</span><span><span class="hljs-variable">$directory</span></span><span>);
<p></span>// Use array_filter to include hidden files<br>
$allFiles = array_filter($files, function($file) {<br>
return $file[0] === '.' || $file !== '.' && $file !== '..';<br>
});</p>
<p>// Output all files and folders (including hidden files)<br>
print_r($allFiles);<br>
</span>

In this example, we use array_filter to filter out files starting with a . (hidden files), while ensuring that . and .. are not included.

Method 2: Combine scandir with glob

Another common approach is to use the glob function, which allows pattern matching to list specific types of files, including hidden files.

<span><span><span class="hljs-variable">$directory</span></span><span> = </span><span><span class="hljs-string">&#039;/path/to/directory&#039;</span></span><span>;
</span><span><span class="hljs-variable">$allFiles</span></span><span> = </span><span><span class="hljs-title function_ invoke__">array_merge</span></span><span>(</span><span><span class="hljs-title function_ invoke__">scandir</span></span><span>(</span><span><span class="hljs-variable">$directory</span></span><span>), </span><span><span class="hljs-title function_ invoke__">glob</span></span><span>(</span><span><span class="hljs-variable">$directory</span></span><span> . </span><span><span class="hljs-string">&#039;/.*&#039;</span></span><span>));
<p></span>print_r($allFiles);<br>
</span>

This way, glob lists all hidden files matching the pattern (starting with .), which are then merged with the results returned by scandir.

4. Filtering and Handling the Returned Files

If you want to perform additional operations on the listed hidden files, such as excluding certain files or sorting them, you can process the array returned by scandir.

For example, to exclude all hidden files starting with .git:

<span><span><span class="hljs-variable">$directory</span></span><span> = </span><span><span class="hljs-string">&#039;/path/to/directory&#039;</span></span><span>;
</span><span><span class="hljs-variable">$files</span></span><span> = </span><span><span class="hljs-title function_ invoke__">scandir</span></span><span>(</span><span><span class="hljs-variable">$directory</span></span><span>);
<p></span>$filteredFiles = array_filter($files, function($file) {<br>
return $file[0] !== '.' || strpos($file, '.git') !== 0;<br>
});</p>
<p>print_r($filteredFiles);<br>
</span>

5. Conclusion

Using the two methods above, you can flexibly use the scandir function to list hidden files. Whether using a custom filter or combining with the glob function, you can easily access all files and folders in a PHP directory, including hidden ones. Choosing the right method in practice allows better control over file reading and management.