How can PHP be used to display files and directories in a directory structure similar to Windows Explorer?
To display files and directories in a directory structure similar to Windows Explorer using PHP, we can use the `scandir()` function to get a list of files and directories in a specified directory. We can then iterate through the list and display them in a structured format using HTML.
<?php
$dir = 'path/to/directory';
$files = scandir($dir);
echo '<ul>';
foreach ($files as $file) {
if ($file != '.' && $file != '..') {
echo '<li>' . $file . '</li>';
if (is_dir($dir . '/' . $file)) {
$subfiles = scandir($dir . '/' . $file);
echo '<ul>';
foreach ($subfiles as $subfile) {
if ($subfile != '.' && $subfile != '..') {
echo '<li>' . $subfile . '</li>';
}
}
echo '</ul>';
}
}
}
echo '</ul>';
?>
Related Questions
- What are the common reasons for headers already sent error in PHP and how can they be prevented in the code?
- How can PHP developers ensure that their code only produces natural number results when using math operators?
- What are the potential benefits or drawbacks of migrating from dBase to SQL for database management?