Reading Directory Contents in PHP
The scandir function allows you to view
the contents of a directory and get an array
of the names of the files and subfolders in it.
The function takes the file path as a parameter.
Let's assume we have a directory dir for example.
Let's look at its contents:
<?php
$files = scandir('dir');
var_dump($files);
?>
In the result array, the scandir function
will also show the presence of directories with the names ".."
and ".". Technically, the first name corresponds
to a link to the parent directory, and the second -
to the current one.
It's better to remove these names from the result array. This is done in the following way:
<?php
$files = scandir('dir');
$files = array_diff($files, ['..', '.']);
var_dump($files);
?>
Can be simplified:
<?php
$files = array_diff(scandir('dir'), ['..', '.']);
var_dump($files);
?>
Let there be a directory dir in the root of your site,
and some text files in it. Display
a column of the names of these files.
Let there be a directory dir in the root of your site,
and some text files in it. Loop through
these files and output their texts to the browser.
Let there be a directory dir in the root of your site,
and some text files in it. Loop through
these files, open each one,
and write an exclamation mark at the end.