PHP file system functions

In this column, we will look at the range of functions PHP provides for interacting with the file system. At their most basic level, file systems interface storage media - generally hard disk drives - with applications. The most popular file systems in current use rely on the directory and the file as their most elementary concepts.

Python vs. PHP: Choosing your next project's language

File system types

There are generally two types of file systems that PHP is used to access: Windows file systems and UNIX-like file systems. The key differences are that UNIX-like file systems can have file and directory owners, group permissions (access permissions for a group of users), read, write and execution permissions, and named links from one file to another. This means that some PHP file system functions may not be supported on some Windows variants, particularly Windows 95 and Windows 98. Consult the PHP manual ( to see if a given function is available for your Windows version.

The other difference is in the separator used to differentiate one directory from another in a file path. Under Windows, a file path looks like this: 'C:\Windows\System\'. Under a UNIX-like system, it would look like this: '/etc/sysconfig/'. A backslash is used with Windows, whereas a forward slash is used with UNIX-like systems.

Retrieving file info

In previous columns we have looked extensively at opening, writing and reading files. What we haven't done is look at information associated with those files.

The following script generates this data and outputs it to the user. To use it, create a file "test.txt" and run the script.

01 <? 02 if(!file_exists("test.txt")) { 03 exit("You must create the file 'test.txt' before running this script"); 04 } 05 $atime = date("r",fileatime("test.txt")); 06 $mtime = date("r",filemtime("test.txt")); 07 $ctime = date("r",filectime("test.txt")); 08 $fs = filesize("test.txt"); 09 $uid = fileowner("test.txt"); 10 $pwd = posix_getpwuid($uid); 11 $owner = $pwd["name"]; 12 $ft = filetype("test.txt"); 13 $perms = fileperms("test.txt"); 14 $permstr = $perms & 0x0100 ? "r" : "-"; 15 $permstr .= $perms & 0x0080 ? "w" : "-"; 16 $permstr .= $perms & 0x0040 ? "x" : "-"; 17 $permstr .= $perms & 0x0020 ? "r" : "-"; 18 $permstr .= $perms & 0x0010 ? "w" : "-"; 19 $permstr .= $perms & 0x0008 ? "x" : "-"; 20 $permstr .= $perms & 0x0004 ? "r" : "-"; 21 $permstr .= $perms & 0x0002 ? "w" : "-"; 22 $permstr .= $perms & 0x0001 ? "x" : "-"; 23 echo "File test.txt\n". 24 "Last accessed: $atime\n". 25 "Last modified: $mtime\n". 26 "Last changed: $ctime\n". 27 "File size: $fs bytes\n". 28 "Owner: $owner\n". 29 "Permissions: $permstr\n". 30 "File type: $ft\n"; 31 ?>

At the start of the file, the script tests to see if "test.txt" exists; if not, it exits. On line 05 the script generates a date-time string of the last time the file was accessed, with the fileatime() function. This function returns a UNIX timestamp (that is, the number of seconds since 1970). This is passed to date() to make a human-readable date-time string. The same is done for the last time the file was modified (with filemtime()) and the last time the file meta data, such as permissions, ownership and so on, was changed (with filectime()).

On line 08, the script retrieves the file size. Lines 09-11 retrieve the username of the file's owner. Line 09 obtains the user ID of the file owner; line 10 looks up the user details in /etc/passwd via posix_getpwuid() (UNIX-like systems only); and, line 11 retrieves the username from the array returned by posix_getpwuid().

On line 12, the script gets the file type information.

Lines 13-22 generate a permissions string for the file commonly used on UNIX-like systems. The string represents the read, write and execute permissions for the file owner, the group the owner belongs to, and everyone else. For example, a permission string of 'rwxr-x---' means that the owner can read, write and execute the file, that users in the same group as the file owner can read and execute the file, and that everyone else has no permission to interact with the file.

Finally, all this information is output to the user.

Join the newsletter!


Sign up to gain exclusive access to email subscriptions, event invitations, competitions, giveaways, and much more.

Membership is free, and your security and privacy remain protected. View our privacy policy before signing up.

Error: Please check your email address.

More about Hewlett-Packard AustraliaHP

Show Comments