Current Location: Home> Latest Articles> How to Fix scandir Not Listing Hidden Files in PHP? Troubleshooting and Solutions

How to Fix scandir Not Listing Hidden Files in PHP? Troubleshooting and Solutions

gitbox 2025-06-11

In PHP, the scandir function is commonly used to read a list of files and directories within a given path. However, you might occasionally run into an issue where scandir fails to list hidden files—those starting with a dot (.). This article analyzes the reasons behind this behavior and shares practical troubleshooting steps and solutions.


1. Why Doesn’t scandir Show Hidden Files?

By default, the scandir function returns all files and directories in a folder, including hidden ones. But if hidden files are missing from the output, here are a few likely reasons:

  1. Hidden Files Were Filtered Out
    Your code may contain conditions that filter out files starting with a dot, inadvertently excluding hidden files.

  2. File Permission Issues
    The PHP environment may lack permission to read hidden files.

  3. Operating System or Filesystem Differences
    Some systems handle hidden files differently, which might cause display inconsistencies depending on the environment.

  4. Special Cases Like Caching or Symbolic Links
    File system caches or symbolic links might interfere with file listings in certain scenarios.


2. How to Properly List Hidden Files in PHP

The scandir function does include hidden files, but its output also contains the special entries . and ... Here’s a basic example:

<?php
$files = scandir('/path/to/directory');
foreach ($files as $file) {
    echo $file . PHP_EOL;
}
?>

This code lists all files, including hidden ones. To exclude . and .., use the following code:

<?php
$files = scandir('/path/to/directory');
foreach ($files as $file) {
    if ($file === '.' || $file === '..') continue;
    echo $file . PHP_EOL;
}
?>

To specifically filter and identify hidden files:

<?php
$files = scandir('/path/to/directory');
foreach ($files as $file) {
    if ($file === '.' || $file === '..') continue;
    if ($file[0] === '.') {
        echo "Hidden file: " . $file . PHP_EOL;
    } else {
        echo "Regular file: " . $file . PHP_EOL;
    }
}
?>

3. Common Troubleshooting Tips

1. Verify the Directory Path

Ensure the path you pass into scandir is correct and accessible. Use is_dir to check:

<?php
$dir = '/path/to/directory';
if (!is_dir($dir)) {
    die("Directory does not exist or is inaccessible");
}
$files = scandir($dir);
?>

2. Confirm PHP Process Permissions

Check whether the PHP user (such as www-data or apache) has read permissions for the directory and its files.

You can check with the is_readable() function:

<?php
if (!is_readable($dir)) {
    die("No read permission for the directory");
}
?>

3. Avoid Filtering Hidden Files in Your Code

Look for logic in your code that might explicitly filter out hidden files. Some projects intentionally hide them for display or security reasons.


4. Bonus Tip: Use FilesystemIterator to Read Hidden Files

Besides scandir, PHP also offers more flexible iterator-based methods, which are especially useful for handling permissions and filtering.

Example:

<?php
$dir = '/path/to/directory';
$iterator = new FilesystemIterator($dir);
<p>foreach ($iterator as $fileinfo) {<br>
$filename = $fileinfo->getFilename();<br>
if ($filename[0] === '.') {<br>
echo "Hidden file: $filename" . PHP_EOL;<br>
} else {<br>
echo "Regular file: $filename" . PHP_EOL;<br>
}<br>
}<br>
?><br>


5. Summary

  • scandir does include hidden files by default; just remember to handle . and ...

  • Ensure the directory path is correct and PHP has permission to read from it.

  • Double-check your code logic to avoid unintentional filtering of hidden files.

  • Use FilesystemIterator for more advanced and flexible file handling.

We hope these tips help you resolve issues with scandir not listing hidden files and streamline your file management tasks in PHP projects.


<?php
$directory = '/var/www/html';
if (!is_dir($directory) || !is_readable($directory)) {
    die("Directory does not exist or is not readable");
}
<p data-is-last-node="" data-is-only-node="">$files = scandir($directory);<br>
foreach ($files as $file) {<br>
if ($file === '.' || $file === '..') continue;<br>
echo $file . PHP_EOL;<br>
}<br>
?><br>