How to write a PHP coding to list out all files and directories as links to them?
Here is the little code snippet on how to list the directories and files with links to them are showing.
// get array of all files and directories in the current directory (it is sorted alphabetically by default)
$dir_entries = scandir(dirname(__FILE__)); // for PHP 5.3+, use __DIR__ instead of dirname(__FILE__)
// split the array of files and directories into two arrays, one containing files and the other directories
$dir_files = array();
$dir_directories = array();
foreach($dir_entries as $dir_entry) {
if(is_dir($dir_entry)) {
array_push($dir_directories, $dir_entry);
} else {
array_push($dir_files, $dir_entry);
}
}
// print directories
foreach($dir_directories as $dir_directory) {
// don't print current and parent directories
if($dir_directory == '.' or $dir_directory == '..') {
continue;
}
echo "<a href=\"" . htmlspecialchars($dir_directory) . "\">" . htmlspecialchars($dir_directory) . "</a><br/>\n";
}
// print files
foreach($dir_files as $dir_file) {
echo "<a href=\"" . htmlspecialchars($dir_file) . "\">" . htmlspecialchars($dir_file) . "</a><br/>\n";
}
$dir_entries = scandir(dirname(__FILE__)); // for PHP 5.3+, use __DIR__ instead of dirname(__FILE__)
// split the array of files and directories into two arrays, one containing files and the other directories
$dir_files = array();
$dir_directories = array();
foreach($dir_entries as $dir_entry) {
if(is_dir($dir_entry)) {
array_push($dir_directories, $dir_entry);
} else {
array_push($dir_files, $dir_entry);
}
}
// print directories
foreach($dir_directories as $dir_directory) {
// don't print current and parent directories
if($dir_directory == '.' or $dir_directory == '..') {
continue;
}
echo "<a href=\"" . htmlspecialchars($dir_directory) . "\">" . htmlspecialchars($dir_directory) . "</a><br/>\n";
}
// print files
foreach($dir_files as $dir_file) {
echo "<a href=\"" . htmlspecialchars($dir_file) . "\">" . htmlspecialchars($dir_file) . "</a><br/>\n";
}
0 Comments
Please add nice comments or answer ....