Current Location: Home> Latest Articles> How to Distinguish Between Directories and Files in scandir Results

How to Distinguish Between Directories and Files in scandir Results

gitbox 2025-09-11

In PHP, the scandir function is used to retrieve a list of files and directories in a specified directory. It returns an array containing the names of all files and subdirectories within that directory. The basic usage of scandir is very simple, but it does not directly provide identifiers to distinguish files from directories. However, we can easily identify the type of each entry by combining it with other functions.

1. Basic Usage of scandir

The syntax of the scandir function is as follows:

<span><span><span class="hljs-keyword">array</span></span><span> </span><span><span class="hljs-title function_ invoke__">scandir</span></span><span> ( </span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$directory</span></span><span> [, </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$sorting_order</span></span><span> = SCANDIR_SORT_ASCENDING [, </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$context</span></span><span> = </span><span><span class="hljs-literal">NULL</span></span><span> ]] )
</span></span>
  • $directory: The path of the directory to scan.

  • $sorting_order: Specifies the sorting order, default is ascending (SCANDIR_SORT_ASCENDING).

  • $context: Optional stream context.

scandir returns an array containing the directory contents, typically filenames and subdirectory names. The return value includes the current directory (.) and parent directory (..) entries.

2. How to Determine Files and Directories?

To distinguish between files and directories returned by scandir, we can use the is_file() and is_dir() functions. They check whether a given path is a file or a directory. By iterating over the results of scandir, we can easily identify the type of each entry.

3. Example Code

Here is an example showing how to use scandir to retrieve a directory list and determine files and directories with is_file() and is_dir():

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-variable">$directory</span></span><span> = </span><span><span class="hljs-string">&#039;/path/to/your/directory&#039;</span></span><span>;  </span><span><span class="hljs-comment">// Set the directory to scan</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>foreach ($files as $file) {<br>
// Skip "." and ".."<br>
if ($file == '.' || </span>$file == '..') {<br>
</span>continue;<br>
}</p>

</span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-title function_ invoke__">is_dir</span></span><span>(</span><span><span class="hljs-variable">$filePath</span></span><span>)) {
    </span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$file</span></span><span> . </span><span><span class="hljs-string">" is a directory\n"</span></span><span>;
} </span><span><span class="hljs-keyword">elseif</span></span><span> (</span><span><span class="hljs-title function_ invoke__">is_file</span></span><span>(</span><span><span class="hljs-variable">$filePath</span></span><span>)) {
    </span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$file</span></span><span> . </span><span><span class="hljs-string">" is a file\n"</span></span><span>;
} </span><span><span class="hljs-keyword">else</span></span><span> {
    </span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$file</span></span><span> . </span><span><span class="hljs-string">" is of unknown type\n"</span></span><span>;
}

}
?>

4. Code Explanation

  1. First, we call scandir($directory) to get all files and directories in the specified directory.

  2. Use a foreach loop to iterate through the returned file list.

  3. For each entry, skip . and .. because they represent the current and parent directories, which usually do not need further processing.

  4. Then, use is_dir() to check if the entry is a directory. If it is, output the corresponding message.

  5. Next, use is_file() to check if it is a file. If it is, output the corresponding message.

  6. If neither condition is met, the entry can be considered a special file (e.g., a symbolic link).

5. Sample Output

Assume that /path/to/your/directory contains the following:

  • file1.txt (file)

  • file2.php (file)

  • subdir1 (directory)

  • subdir2 (directory)

Running the above code may produce the following output:

<span><span>file1.txt is a file
file2.php is a file
subdir1 is a directory
subdir2 is a directory
</span></span>

6. Conclusion

By combining scandir with is_file() and is_dir(), we can easily determine the type of entries in the file system, effectively distinguishing between files and directories. Although scandir does not provide direct identifiers, using it alongside other file system functions makes this task straightforward.