6.2 Checking File Permissions


You want to get the file permissions on a file.

Technique

Use the fileperms() function to accurately get the number value of the permissions, or use the is_* ( is_dir() , is_executable() , is_file() , is_link() , is_readable() , and is_writable() ) functions to get human-usable results.

Using fileperms() :

 <?php if ((fileperms ($fn) & 0777) != 0644) {     chmod ($fn, 0644); // rw-r--r- } ?> 

Using the is_* functions:

 <?php if (!is_readable ($fn)) {     chmod ($fn, 0777); // rwxrwxrwx } ?> 

Comments

The fileperms() function returns the current permissions numerically (in octal form). This is a more advanced way of checking the file permissions. The bits making up the permissions returned by fileperms() are described in more detail in the man page for the mknod() function in UNIX. If you simply want to check whether the file is available for read access, write access, and so on, use the is_* functions.

Please be aware that these functions are costly on the system, and therefore they are cached. To make multiple calls on the same file, you need to use the clearstatcache() function. For more information see recipe 6.7.



PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

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