Hudzilla.org - the homepage of Paul Hudson
Contents > Files Wish List | Report Bug | About Me ]

8.7     Retrieving a file's status: is_readable(), is_writeable(), is_executable(), is_file(), is_dir(), and clearstatcache()

This is NOT the latest copy of this book; click here for the latest version.

bool is_readable ( string filename)

bool is_writeable ( string filename)

bool is_executable ( string filename)

bool is_file ( string filename)

bool is_dir ( string filename)

void clearstatcache ( void )

If you're sick of getting errors when you try to work with a file to which you have no permissions, there is a solution: is_readable() and its cousin functions, is_writeable(), is_executable(), is_file(), and is_dir(), takes a string as its only parameter, and either returns true or false. Naturally is_readable() will return true if the string parameter is readable, is_dir() will return false if the parameter is not a directory, etc.

Here is an example:

<?php
    
if (is_file($filename) && is_writeable($filename)) {
        
$handle = fopen($filename, "w+");
        ...[
snip]...
    }
?>

Is_readable() and friends have their results cached for speed purposes - if you call is_file() on a filename several times in a row, PHP will calculate it the first time around then used the same value again and again in the future. If you want to clear this cached data so that PHP will have to check is_file() properly, you need to use the clearstatcache() function.

Calling clearstatcache() wipes PHP's file information cache, forcing it recalculate is_file(), is_readable() and such afresh. Clearstatcache() is therefore particularly useful if you are checking a file several times in a script and are aware that that file might change status during execution. Clearstatcache() takes no parameters and returns no value.

Author's Note: is_readable(), is_writeable(), is_executable(), is_file(), and is_dir() will all fail to work for remote files, as the file/directory to be examined must be local to the web server so that it can check it properly.





<< 8.6 Checking whether a file exists: file_exists()   8.8 Dissecting filename information: pathinfo() and basename() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Be the first to add a comment to this chapter!



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is eight plus four?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow