Wednesday, December 23, 2009

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.How to write a PHP coding to list out all files and directories as links to them?
// 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 ';%26lt;a href=\';'; . htmlspecialchars($dir_directory) . ';\';%26gt;'; . htmlspecialchars($dir_directory) . ';%26lt;/a%26gt;%26lt;br/%26gt;\n';;


}





// print files


foreach($dir_files as $dir_file) {


聽聽聽聽echo ';%26lt;a href=\';'; . htmlspecialchars($dir_file) . ';\';%26gt;'; . htmlspecialchars($dir_file) . ';%26lt;/a%26gt;%26lt;br/%26gt;\n';;


}How to write a PHP coding to list out all files and directories as links to them?
here are some greate video tutorials: http://www.phptutorials.net/ Report Abuse

No comments:

Post a Comment