This simple files will produce a list of all files below a particular directory, and provide a link to that particular file. The first function returns an array... and the second loops through the result and creates a list.
1
<?php
2
/*
3
Print all files from and below a directory with PHP
4
http://www.beliefmedia.com/code/php-snippets/print-files
5
*/
6
7
function beliefmedia_list_directory_array($dir = '') {
8
9
/* Start directory */
10
if ($dir == '') $dir = '.';
11
12
/* Name of this file */
13
14
15
/* If invalid directory */
16
17
18
$files = array();
19
20
21
22
23
/* Loop through files, skipping . and .. recursing if necessary */
24
25
26
if ($file != "Thumb.db" && $file != "Thumbs.db" && $file != $filename) $filepath = $dir . '/' . $file;
27
28
29
30
}
31
32
/* Close directory handle */
33
closedir($fh);
34
35
return (array) $files;
36
}
The second function takes the resulting array and prints a list.
1
<?php
2
/*
3
Print all files from and below a directory with PHP
4
Creates a basic list of all files from array
5
http://www.beliefmedia.com/code/php-snippets/print-files
6
*/
7
8
function beliefmedia_list_directory($dir = '.') {
9
$files = beliefmedia_list_directory_array($dir);
10
foreach($files as $file) {
11
echo '<a href="' . $file . '">' . $file . '</a>';
12
}
13
}
Usage is as follows: