Skip to Content
PHP Cookbook
book

PHP Cookbook

by David Sklar, Adam Trachtenberg
November 2002
Intermediate to advanced
640 pages
16h 33m
English
O'Reilly Media, Inc.
Content preview from PHP Cookbook

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.

Web listing

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP Cookbook

PHP Cookbook

Eric A. Mann
PHP Cookbook, 2nd Edition

PHP Cookbook, 2nd Edition

Adam Trachtenberg, David Sklar
PHP Cookbook, 3rd Edition

PHP Cookbook, 3rd Edition

David Sklar, Adam Trachtenberg
Programming PHP

Programming PHP

Rasmus Lerdorf, Kevin Tatroe

Publisher Resources

ISBN: 1565926811Supplemental ContentCatalog PageErrata