Home >Backend Development >PHP Tutorial >How Can I List All Files in a Directory Using PHP?

How Can I List All Files in a Directory Using PHP?

Barbara Streisand
Barbara StreisandOriginal
2024-11-22 15:09:46880browse

How Can I List All Files in a Directory Using PHP?

Obtaining File Listings with PHP

Question: How can you effectively list all files within a specified directory using PHP?

Answer:

To retrieve a list of files contained within a directory, PHP provides the scandir() function. This function takes the directory path as its input parameter and returns an array containing the file names.

Implementation Example:

$directory = 'usernames/';
$files = scandir($directory);

The $files array now contains a list of file names within the specified directory.

Additional Note:

If you wish to exclude the special directories '.' and '..' from the list, use the following approach:

$files = array_diff(scandir($directory), array('.', '..'));

The above is the detailed content of How Can I List All Files in a Directory Using PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn