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!

Error: Please check your email address.
Rocket to Success - Your 10 Tips for Smarter ERP System Selection
Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments



Victorinox Werks Professional Executive 17 Laptop Case

Learn more >



Back To Business Guide

Click for more ›

Brand Post

Most Popular Reviews

Latest Articles


PCW Evaluation Team

Louise Coady

Brother MFC-L9570CDW Multifunction Printer

The printer was convenient, produced clear and vibrant images and was very easy to use

Edwina Hargreaves

WD My Cloud Home

I would recommend this device for families and small businesses who want one safe place to store all their important digital content and a way to easily share it with friends, family, business partners, or customers.

Walid Mikhael

Brother QL-820NWB Professional Label Printer

It’s easy to set up, it’s compact and quiet when printing and to top if off, the print quality is excellent. This is hands down the best printer I’ve used for printing labels.

Ben Ramsden

Sharp PN-40TC1 Huddle Board

Brainstorming, innovation, problem solving, and negotiation have all become much more productive and valuable if people can easily collaborate in real time with minimal friction.

Sarah Ieroianni

Brother QL-820NWB Professional Label Printer

The print quality also does not disappoint, it’s clear, bold, doesn’t smudge and the text is perfectly sized.

Ratchada Dunn

Sharp PN-40TC1 Huddle Board

The Huddle Board’s built in program; Sharp Touch Viewing software allows us to easily manipulate and edit our documents (jpegs and PDFs) all at the same time on the dashboard.

Featured Content

Product Launch Showcase

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?