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 (www.php.net/manual/en/ref.filesystem.php) 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.

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

Gavin Sherry

PC World
Comments are now closed.

Latest News Articles

Most Popular Articles

Follow Us

GGG Evaluation Team

Kathy Cassidy

STYLISTIC Q702

First impression on unpacking the Q702 test unit was the solid feel and clean, minimalist styling.

Anthony Grifoni

STYLISTIC Q572

For work use, Microsoft Word and Excel programs pre-installed on the device are adequate for preparing short documents.

Steph Mundell

LIFEBOOK UH574

The Fujitsu LifeBook UH574 allowed for great mobility without being obnoxiously heavy or clunky. Its twelve hours of battery life did not disappoint.

Andrew Mitsi

STYLISTIC Q702

The screen was particularly good. It is bright and visible from most angles, however heat is an issue, particularly around the Windows button on the front, and on the back where the battery housing is located.

Simon Harriott

STYLISTIC Q702

My first impression after unboxing the Q702 is that it is a nice looking unit. Styling is somewhat minimalist but very effective. The tablet part, once detached, has a nice weight, and no buttons or switches are located in awkward or intrusive positions.

Resources

Best Deals on GoodGearGuide

Latest Jobs

Don’t have an account? Sign up here

Don't have an account? Sign up now

Forgot password?