Recipe 24.2. Getting File Information


24.2.1. Problem

You want to read a file's metadata'for example, permissions and ownership.

24.2.2. Solution

Use stat( ), as in Example 24-8, which returns an array of information about a file.

Getting file information

<?php $info = stat('harpo.php'); ?>

24.2.3. Discussion

stat( ) returns an array with both numeric and string indexes with information about a file. The elements of this array are in Table 24-3.

Table 24-3. Information returned by stat( )

Numeric index

String index

Value

0

dev

Device

1

ino

Inode

2

mode

Permissions

3

nlink

Link count

4

uid

Owner's user ID

5

gid

Group's group ID

6

rdev

Device type for inode devices (-1 on Windows)

7

size

Size (in bytes)

8

atime

Last access time (epoch timestamp)

9

mtime

Last change time of contents (epoch timestamp)

10

ctime

Last change time of contents or metadata (epoch timestamp)

11

blksize

Block size for I/O (-1 on Windows)

12

blocks

Number of blocks allocated to this file


The mode element of the returned array contains the permissions expressed as a base 10 integer. This is confusing since permissions are usually either expressed symbolically (e.g., ls's -rw-r--r-- output) or as an octal integer (e.g., 0644). To convert the permissions to a more understandable format, use base_convert( ) to change the permissions to octal, as shown in Example 24-9.

Converting file permission values

<?php $file_info = stat('/tmp/session.txt'); $permissions = base_convert($file_info['mode'],10,8); ?>

In Example 24-9, $permissions is a six-digit octal number. For example, if ls displays the following about /tmp/session.txt:

-rw-rw-r--    1 sklar    sklar          12 Oct 23 17:55 /tmp/session.txt

Then $file_info['mode'] is 33204 and $permissions is 100664. The last three digits (664) are the user (read and write), group (read and write), and other (read) permissions for the file. The third digit, 0, means that the file is not setuid or setgid. The leftmost 10 means that the file is a regular file (and not a socket, symbolic link, or other special file).

Because stat( ) returns an array with both numeric and string indexes, using foreach to iterate through the returned array produces two copies of each value. Instead, use a for loop from element 0 to element 12 of the returned array.

Calling stat( ) on a symbolic link returns information about the file the symbolic link points to. To get information about the symbolic link itself, use lstat( ).

Similar to stat( ) is fstat( ), which takes a filehandle (returned from fopen( ) or popen( )) as an argument.

PHP's stat( ) function uses the underlying stat(2) system call, which is expensive. To minimize overhead, PHP caches the result of calling stat(2). So if you call stat( ) on a file, change its permissions, and call stat( ) on the same file again, you get the same results. To force PHP to reload the file's metadata, call clearstatcache( ), which flushes PHP's cached information. PHP also uses this cache for the other functions that return file metadata: file_exists( ), fileatime( ), filectime( ), filegroup( ), fileinode( ), filemtime( ), fileowner( ), fileperms( ), filesize( ), filetype( ), fstat( ), is_dir( ), is_executable( ), is_file( ), is_link( ), is_readable( ), is_writable( ), and lstat( ).

24.2.4. See Also

Documentation on stat( ) at http://www.php.net/stat, lstat( ) at http://www.php.net/lstat, fstat( ) at http://www.php.net/fstat, and clearstatcache( ) at http://www.php.net/clearstatcache.




PHP Cookbook, 2nd Edition
PHP Cookbook: Solutions and Examples for PHP Programmers
ISBN: 0596101015
EAN: 2147483647
Year: 2006
Pages: 445

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net