List files and directories example
Here are a few examples of how to list files and directories in PHP:
**Using `glob()` function:**
php
$files = glob('./path/to/directory/*');
$directories = glob('./path/to/directory/*/', GLOB_ONLYDIR);
print_r($files);
print_r($directories);
The first example will return an array of all files and directories in the specified directory. The second example will only return an array of
directories.
**Using `scandir()` function:**
php
$directory = './path/to/directory/';
$files = scandir($directory);
print_r($files);
This will return an array of all files and directories in the specified directory, including dot files (e.g. `.htaccess`).
**Using Recursive Directory Iterator:**
php
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('./path/to/directory/'));
foreach ($iterator as $file) {
echo $file->getPathname() . "\n";
}
This will recursively iterate over all files and directories in the specified directory, printing out each file's path.
**Using `getcwd()` function to list current working directory:**
php
$directory = getcwd();
$files = scandir($directory);
print_r($files);
This will return an array of all files and directories in the current working directory.