» Home
Tuesday, September 26, 2017 - 03:55:12

How to write a PHP coding to list out all files and directories as links to them?

How to write a PHP coding to list out all files and directories as links to them?

This is somewhat similar to some index pages. When new file or folder is added to the directory, HTML page should display the newly created file/folder together with previous ones after it is being refreshed. (prefer in alphabatical order)

How to achieve this sort of functionality in PHP? Please provide sample coding as well. (and any references)

Thanks.

Asked by demouser 5 years ago In Programming, answers (1), visited (1153)

php coding file

Posted on : Sunday, February 3, 2013, 07:34:39 In Programming

niki33

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";
}
Answered: Sunday, February 3, 2013, 07:51:32, 5 years ago User Rating: 0  Votes: +0 -0
Last Questions

What is enigma?

I am confused about word enigma. What is that? Posted on : Thursday, August 31, 2017, 22:45:55

What's the worst Star Wars film?

Episode 1 Episode 2 Episode 3 Episode 4 Episode 5 Episode 6 Episode 7 Posted on : Tuesday, January 17, 2017, 13:07:53

ce filme va plac?

voua ce filme va plac sau urmariti in general? Posted on : Sunday, November 27, 2016, 14:29:46

What is the best programming language to start with?

Hello! I am wanting to learn programming, but I dont know which language I should start off... Posted on : Saturday, November 12, 2016, 02:35:09

How old James bond ?

Hi , How old James bond ? Any body know ? Posted on : Wednesday, March 30, 2016, 21:37:39

What is social bookmarking?

Can somebody explain me what mean social bookmarking? Is good for traffic to website or what?... Posted on : Sunday, August 23, 2015, 10:26:36

What is the best way to play guitar?

Want to know who knows how to play guitar Posted on : Monday, July 13, 2015, 21:03:39

what is dreamweaver

i want to know about the software of adobe named dremaweaver? Posted on : Tuesday, August 12, 2014, 17:03:41