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
8
function beliefmedia_list_directory_array($dir = '') {
9
10
/* Start directory */
11
if ($dir == '') $dir = '.';
12
13
/* Name of this file */
14
15
16
/* If invalid directory */
17
18
19
$files = array();
20
21
22
23
24
/* Loop through files, skipping . and .. recursing if necessary */
25
26
27
if ($file != "Thumb.db" && $file != "Thumbs.db" && $file != $filename) $filepath = $dir . '/' . $file;
28
29
30
31
}
32
33
/* Close directory handle */
34
35
36
return (array) $files;
37
}
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
9
function beliefmedia_list_directory($dir = '.') {
10
$files = beliefmedia_list_directory_array($dir);
11
foreach($files as $file) {
12
echo '<a href="' . $file . '">' . $file . '</a><br>';
13
}
14
}
Usage is as follows: