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 PC World newsletter!

Error: Please check your email address.

Our Back to Business guide highlights the best products for you to boost your productivity at home, on the road, at the office, or in the classroom.

Keep up with the latest tech news, reviews and previews by subscribing to the Good Gear Guide newsletter.

Gavin Sherry

PC World
Show Comments

Most Popular Reviews

Latest News Articles


PCW Evaluation Team

Azadeh Williams

HP OfficeJet Pro 8730

A smarter way to print for busy small business owners, combining speedy printing with scanning and copying, making it easier to produce high quality documents and images at a touch of a button.

Andrew Grant

HP OfficeJet Pro 8730

I've had a multifunction printer in the office going on 10 years now. It was a neat bit of kit back in the day -- print, copy, scan, fax -- when printing over WiFi felt a bit like magic. It’s seen better days though and an upgrade’s well overdue. This HP OfficeJet Pro 8730 looks like it ticks all the same boxes: print, copy, scan, and fax. (Really? Does anyone fax anything any more? I guess it's good to know the facility’s there, just in case.) Printing over WiFi is more-or- less standard these days.

Ed Dawson

HP OfficeJet Pro 8730

As a freelance writer who is always on the go, I like my technology to be both efficient and effective so I can do my job well. The HP OfficeJet Pro 8730 Inkjet Printer ticks all the boxes in terms of form factor, performance and user interface.

Michael Hargreaves

Windows 10 for Business / Dell XPS 13

I’d happily recommend this touchscreen laptop and Windows 10 as a great way to get serious work done at a desk or on the road.

Aysha Strobbe

Windows 10 / HP Spectre x360

Ultimately, I think the Windows 10 environment is excellent for me as it caters for so many different uses. The inclusion of the Xbox app is also great for when you need some downtime too!

Mark Escubio

Windows 10 / Lenovo Yoga 910

For me, the Xbox Play Anywhere is a great new feature as it allows you to play your current Xbox games with higher resolutions and better graphics without forking out extra cash for another copy. Although available titles are still scarce, but I’m sure it will grow in time.

Featured Content

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?