How to List All Files in a Directory with PHP?

Mary-Kate Olsen
Release: 2024-11-22 16:54:22
Original
404 people have browsed it

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);
Copy after login

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('.', '..'));
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template