19.12. Program: Web Server Directory Listing
The web-ls.php program shown in Example 19-4 provides a view of the files inside your web server’s document root, formatted like the output of the Unix command ls. Filenames are linked so that you can download each file, and directory names are linked so that you can browse in each directory, as shown in Figure 19-1.
Figure 19-1. Web listing
Most lines in Example 19-4 are devoted to building an
easy-to-read representation of the file’s
permissions, but the guts of the program are in the
while
loop at the end. The $d->read( )
method gets the name of each file in
the directory. Then, lstat( )
retrieves
information about that file, and printf( )
prints
out the formatted information about that file.
The mode_string( )
functions and the constants it uses
turn the octal representation of a file’s mode
(e.g., 35316
) into an easier-to-read string (e.g.,
-rwsrw-r--
).
Example 19-4. web-ls.php
/* Bit masks for determining file permissions and type. The names and values * listed below are POSIX-compliant, individual systems may have their own * extensions. */ define('S_IFMT',0170000); // mask for all types define('S_IFSOCK',0140000); // type: socket define('S_IFLNK',0120000); // type: symbolic link define('S_IFREG',0100000); // type: regular file define('S_IFBLK',0060000); // type: block device define('S_IFDIR',0040000); // type: directory ...
Get PHP Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.