-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir-listing-20230320-0419.php
84 lines (80 loc) · 2.29 KB
/
dir-listing-20230320-0419.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/*
examples used in this code come from
<https://www.w3schools.com/php/func_directory_scandir.asp>
<https://www.w3schools.com/php/func_string_printf.asp>
*/
/* define the field widths to use for directory listing lines */
$fldwid = array(
'bytes' => "12", /* 12 digits for file size in bytes */
'perms' => "4", /* 4 digits for permissions */
'times' => "16", /* 16 characters for date & time */
'names' => "", /* filename width is not defined */
);
/* define the format for the direcotry listing lines */
$fmtstr=
"%".$fldwid['bytes']."s ".
"%".$fldwid['times']."s ".
"%".$fldwid['names']."s\n";
/*
add an underline to the listing header line before we
add it to the outpput array
*/
$dirlsthdr = '<span style="text-decoration:underline;">'.
sprintf( $fmtstr,
"bytes",
"ctime",
"filename").
'</span>';
$lines = array( $dirlsthdr ); /* add the header to output lines */
$datefmtstr = "Ymd:His"; /* date format string */
$cwd = getcwd(); /* get name of current working dir */
$files = scandir($cwd); /* get the list files */
/*
iterate through the files in this directory
*/
foreach ($files as $fn) {
/* skip the '.' & '..' entries */
if (( $fn === '.' ) || ( $fn === '..' )) { continue; }
/*
append formatted output lines to output array
*/
$lines[] = sprintf($fmtstr,
filesize($fn), /* get the filesize */
date( $datefmtstr, filectime($fn)),
$fn
);
}
/* **********************************************************
## KEEP ALL HTML Below this line
* **********************************************************
output an HTML document with the directory listing
TODO: The PHP Code should be independent of the HTML
output code. All HTML should be provided by
templates which may instanciate the PHP
object(s).
*/
echo '<!DOCTYPE html>
<html lang="en">
<head>
<title>'.
$cwd.
'</title>
</head>
';
/*
open the body element
*/
echo '<body>
<code>
<pre>
';
/* add the directory lines */
foreach ($lines as $ln) {
echo ' '. $ln.'';
}
echo ' </pre>
</code>
</body>
</html>';
?>