PHP file system functions
- 21 October, 2003 07:05
- Comments
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.
- Bookmark this page
- Share this article
- Got more on this story? Email Computerworld
- Follow Computerworld on twitter
- Reconciling Datacenter consolidation and security: It starts with an integrated approach
- Automating Your Processes to Outperform Your Competition
- Securing SOA and Web Services with Oracle Enterprise Gateway
- Customer Case Study: Yarra Valley Water Turns to Enterprise Software to Improve Information Flow
- Workshifting: a global market research report
-
Change My Password logs 10 millionth account
-
NBN service plans won't cost consumers more: Conroy
-
Spotify music streaming hits Australian shores
-
Don't use Emacs, says Java's father
-
Brain drain: Where Cobol systems go from here
-
Windows 7 for Dummies® Dvd+book Bundle
-
Windows 7 for Dummies®
-
MYOB Software for Dummies 6E Australian Edition
-
Teach Yourself Visually Windows 7
-
Microsoft Office
-
Computers for Seniors for Dummies, 2nd Edition
-
Windows 7 for Seniors for Dummies®
-
Excel 2007 All-In-One Desk Reference for Dummies
-
Office 2007 for Dummies









Comments
Post new comment