Home >Backend Development >PHP Tutorial >How to List All Files in a Directory with PHP?

How to List All Files in a Directory with PHP?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-22 16:54:22529browse

How to List All Files in a Directory with PHP?

How to List All Files in a Directory Using PHP

Question:

How can I list all the files in a specific directory using PHP? Is there a built-in function that allows me to do this?

Answer:

To list all the files within a directory, you can utilize the scandir() function in PHP.

$path    = '/tmp';
$files = scandir($path);

The $files array will now contain a list of the files in the /tmp directory.

Additional Tip:

To remove special directories like "." and ".." from the results, you can employ the following code:

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

The above is the detailed content of How to List All Files in a Directory with 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