Home  >  Article  >  Backend Development  >  Count the number of files in a directory in PHP

Count the number of files in a directory in PHP

PHPz
PHPzforward
2024-02-28 11:02:01406browse

php editor Yuzai teaches you how to count the number of files in a directory in PHP. By using PHP's filesystem functions, you can easily iterate through a directory and count the number of files within it. This can be a very useful feature in some projects, such as counting the number of images or documents on a website. Next, we will detail how to use PHP code to achieve this functionality. Let’s learn together!


Use the FilesystemIterator class in php to count the number of files in a directory

PHP provides the FilesystemIterator class, which is an elegant way to count files in a directory. This class contains a number of functions and constants that perform various operations for iterating the file system.

Use the iterator_count() function to count the number of elements in an iterator.

Let us consider the following directory structure.

<code>
<code class="language-bash hljs">myDir
├── abc.<strong class="keylink">html</strong>
├── abc.jpeg
├── abc.php
└── abc.png
</code></code>

First, create a variable $iterator and store an instance of FilesystemIterator(). Set the first parameter to __DIR__ and use FilesystemIterator::SKIP_DOTS as the second parameter in the constructor.

__DIR__ indicates the current directory where the script is located, and the second parameter is a flag indicating skipping. and .. exist in the directory. $iterator Variable contains the file system iterator of the current directory.

It can be iterated over to get information such as file name, number of files, etc.

Next, use the iterator_count() function to print the number of $iterator in the directory.

Sample code:

<code>
<code class="language-php hljs"><span class="hljs-variable">$iterator</span> = <span class="hljs-keyword">new</span> <span class="hljs-built_in">FilesystemIterator</span>(<span class="hljs-keyword">__DIR__</span>, <span class="hljs-built_in">FilesystemIterator</span>::SKIP_DOTS);
<span class="hljs-keyword">echo</span> <span class="hljs-string">"There are "</span>. iterator_count(<span class="hljs-variable">$iterator</span>). <span class="hljs-string">" files in the directory"</span>;
</code></code>

The output results are as follows:

<code>
<code class="language-bash hljs">There are 4 files <span class="hljs-keyword">in</span> the directory
</code></code>

Therefore, we can use the FilesystemIterator() class to count the number of files in the directory.


Count the number of files in a directory using the glob() and count() functions in PHP

We can use the glob() and count() functions to get the number of files in a directory. glob() The function gives an array of files and folders that match the specified pattern.

We can use * to get all files in a directory. Next, we can use the count() function to get the number of elements in the array.

For example, create a $path variable to store the exact path of the directory where the files are to be counted. Next, use the glob() function on the $path variable and concatenate that variable with /*.

As a result, an array of all files in myDir is created. Finally, use the count() function on the result of the glob() function.

In this way, you can check the number of files in the myDir directory. The following code example represents the file structure used in the first example.

Sample code:

<code>
<code class="language-php hljs"><span class="hljs-variable">$path</span> =<span class="hljs-variable">$_SERVER</span>[<span class="hljs-string">'DOCUMENT_ROOT'</span>].<span class="hljs-string">"/myDir"</span>;
<span class="hljs-variable">$num</span> = count(glob(<span class="hljs-variable">$path</span> . <span class="hljs-string">"/*"</span>));
<span class="hljs-keyword">echo</span> <span class="hljs-variable">$num</span>;
</code></code>

The output results are as follows:

<code>
<code class="language-bash hljs">4
</code></code>

Count the number of files in a PHP directory using scandir() and count() functions

This method is similar to the second method. The only difference here is that we use the scandir() function instead of the glob() function.

We can provide the directory path as a parameter to the scandir() function. This function returns an array containing all the contents of the directory, including . and ...

Next, we can use the count() function to get the number of contents in the directory. Finally, the important part is to subtract 2 from the result of the count() function to remove the counts of . and ....

Therefore, we can get the exact number of files in the directory.

Sample code:

<code>
<code class="language-php hljs"><span class="hljs-variable">$path</span> =<span class="hljs-variable">$_SERVER</span>[<span class="hljs-string">'DOCUMENT_ROOT'</span>].<span class="hljs-string">"/myDir"</span>;
<span class="hljs-variable">$num</span> = count(scandir(<span class="hljs-variable">$path</span>))-<span class="hljs-number">2</span>;
<span class="hljs-keyword">echo</span> <span class="hljs-string">"there are <span class="hljs-subst">$num</span> files in the directory"</span>;
</code></code>

The output results are as follows:

<code>
<code class="language-bash hljs">there are 4 files <span class="hljs-keyword">in</span> the directory
</code></code>

The above is the detailed content of Count the number of files in a directory in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete