How to list the contents of a directory in PHP?

To list the contents of a directory in PHP, you can use the scandir() function. Here's an example:

$directory = '/path/to/directory'; // List all files and directories in the specified directory $contents = scandir($directory); // Loop through the list of contents foreach ($contents as $file) { // Exclude the current and parent directories if ($file != '.' && $file != '..') { echo $file . "<br>"; } }

Make sure to replace /path/to/directory with the actual path of the directory you want to list. The scandir() function returns an array with the names of all files and directories in the given directory. By excluding . and .., you can avoid displaying the current and parent directories.