List all files in one directory PHP

To list all files in a directory using PHP, you can use the glob() function. This function returns an array of filenames that match a specified pattern.

For example, to list all files in the /path/to/directory directory, you can use the following code:

<?php

$files = glob('/path/to/directory/*');

This will return an array of filenames for all files in the directory.

Watch a course Learn object oriented PHP

You can then use a foreach loop to iterate over the array and perform operations on the files, such as printing their names or reading their contents:

<?php

foreach($files as $file) {
  echo $file . "\n";
}

You can also use the glob() function to filter the files by extension. For example, to list only PHP files, you can use the following code:

<?php

$php_files = glob('/path/to/directory/*.php');

This will return an array of filenames for all PHP files in the directory.

You can use other patterns in the glob() function to match different types of files. For example, the pattern *.{php,txt} will match all PHP and text files in the directory.

I hope this helps! Let me know if you have any questions.