Current Location: Home> Latest Articles> How to Use scandir and filetype to Determine File Types and Categorize Files?

How to Use scandir and filetype to Determine File Types and Categorize Files?

gitbox 2025-07-17

When developing PHP applications, managing files in the filesystem is often an essential task. To handle these files more efficiently, we can use PHP's scandir function to read directories, combined with the filetype function to determine the file types, allowing us to categorize files accordingly. This article will provide a detailed explanation of how to achieve file type classification using these two functions.

1. Introduction to the scandir Function

The scandir function is used to get all files and subdirectories within a specified directory, returning an array containing the file names. Its basic syntax is as follows:

<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><span class="hljs-keyword">array</span></span><span>|</span><span><span class="hljs-literal">false</span></span><span>
</span></span>
  • $directory: The path of the directory to read.

  • $sorting_order: Sorting order, default is ascending (SCANDIR_SORT_ASCENDING), descending sorting can be specified using SCANDIR_SORT_DESCENDING.

  • $context: Resource context, usually not required.

Using scandir, it is easy to list all files and folders within a directory.

2. Introduction to the filetype Function

The filetype function returns the file type of a given path. Its return value can be one of the following types:

  • 'file': Regular file

  • 'dir': Directory

  • 'link': Symbolic link

  • 'unknown': Unknown type (file inaccessible or incorrect path)

The basic syntax is as follows:

<span><span><span class="hljs-title function_ invoke__">filetype</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$filename</span></span><span>): </span><span><span class="hljs-keyword">string</span></span><span>|</span><span><span class="hljs-literal">false</span></span><span>
</span></span>
  • $filename: The file or directory path to check.

3. Using scandir and filetype Together for File Categorization

We can combine scandir and filetype to iterate through all files and subdirectories in a directory and classify them based on their file types. Here is a simple example:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Define the directory path</span></span>
</span><span><span class="hljs-variable">$directory</span>
</span></span>