Getting File System Data with PHP

In this third part of an eight-part article series on using PHP to work with the file and operating system, you’ll learn how to retrieve the size of a directory and find out a file’s last access and modification times. This article is excerpted from chapter 10 of the book Beginning PHP and PostgreSQL 8: From Novice to Professional, written by W. Jason Gilmore and Robert H. Treat (Apress; ISBN: 1590595475).

Retrieving a Directory Size

PHP doesn’t currently offer a standard function for retrieving the total size of a directory, a task more often required than retrieving total disk space (see disk_total_space()). And although you could make a system-level call to du using exec() or system() (both of which are introduced later in this chapter), such functions are often disabled for security reasons. The alternative solution is to write a custom PHP function that is capable of carrying out this task. A recursive function seems particularly well-suited for this task. One possible variation is offered in Listing 10-2.

Note The du command will summarize disk usage of a file or directory. See the appropriate man page for usage information.

Listing 10-2. Determining the Size of a Directory’s Contents

function directory_size($directory) {

/* Open the directory and read its contents. */
if ($dh = @opendir($directory)) {

/* Iterate through each directory entry. */
while (($filename = readdir ($dh))) {

/* Filter out some of the unwanted directory entries. */
if ($filename != "." && $filename != "..")

// File, so determine size and add to total.
if (is_file($directory."/".$filename))
$directorySize += filesize($directory."/".$filename);

// New directory, so initiate recursion. */
if (is_dir($directory."/".$filename))
$directorySize += directory_size($directory."/".$filename);
} #endWHILE
} #endIF

return $directorySize;

} #end directory_size()

$directory = "/usr/local/apache2/htdocs/book/chapter10/";
$totalSize = round((directory_size($directory) / 1024), 2);
echo "Directory $directory: ".$totalSize. "kb.";


{mospagebreak title=Access and Modification Times}

The ability to determine a file’s last access and modification time plays an important role in many administrative tasks, especially in Web applications that involve network or CPU-intensive update operations. PHP offers three functions for determining a file’s access, creation, and last modification time, all of which are introduced in this section.


int fileatime (string filename)

The fileatime() function returns filename’s last access time in Unix timestamp format, or FALSE on error. An example follows:

$file = "/usr/local/apache2/htdocs/book/chapter10/stat.php";
echo "File last accessed: ".date("m-d-y g:i:sa", fileatime($file));

This returns:

——————————————–File last accessed: 06-09-03 1:26:14pm


int filectime (string filename)

The filectime() function returns filename’s last changed time in Unix timestamp format, or FALSE on error. An example follows:

$file = "/usr/local/apache2/htdocs/book/chapter10/stat.php";
echo "File inode last changed: ".date("m-d-y g:i:sa", fileatime($file));

This returns:

——————————————–File inode last changed: 06-09-03 1:26:14pm

Note The "last changed time" differs from the "last modified time" in that the last changed time refers to any change in the file’s inode data, including changes to permissions, owner, group, or other inode-specific information, whereas the last modified time refers to changes to the file’s content (specifically, byte size).


int filemtime (string filename)

The filemtime() function returns filename’s last modification time in Unix timestamp format, or FALSE otherwise. The following code demonstrates how to place a "last modified timestamp" on a Web page:

$file = "/usr/local/apache2/htdocs/book/chapter10/stat.php";
echo "File last updated: ".date("m-d-y g:i:sa", filemtime($file));

This returns:

——————————————–File last updated: 06-09-03 1:26:14pm

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye