Home > Backend Development > PHP Tutorial > How to List Only Files with a Specific Extension in a Directory Using PHP?

How to List Only Files with a Specific Extension in a Directory Using PHP?

Barbara Streisand
Release: 2024-11-29 15:26:11
Original
859 people have browsed it

How to List Only Files with a Specific Extension in a Directory Using PHP?

PHP: Listing Specific Files in a Directory

The PHP code you provided is a simple way to list all the files in a directory. However, what if you only want to list files with a specific extension, such as .xml? Here's how you can modify the code:

In this modified code:

  • The glob() function is used to retrieve a list of files matching the specified pattern, which is '/path/to/dir/*.xml' in this case.
  • The resulting array of filenames is stored in the $files variable.
  • A loop iterates through the array and outputs each filename in an HTML list.

This code will only list files that end with the .xml extension in the specified directory.

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