The Is_Readable PHP Function
PHP has a lot of tools for working with files, and it's important to learn which ones best suit your needs. For example, if you're going to be parsing input for a specific format like HTML or XML, using fscanf or the PECL xml_parse might make more sense than just file.
But even if you're not dealing with formats, it's still a good idea to use is_readable or some other form of checking that your file is readable by PHP before passing it to any other functions that might access its contents. Consider what would happen if an uploaded file is missing, or it has changed permissions: if PHP is unable to read it, the entire system might halt. In this case, a better exit strategy might be to serve a 404 or some other simple error message.
It may not seem important to write safer file-access code — after all, most php applications are for the web and run in controlled environments where the filesystem is predictable. But I've seen clients lose tens of thousands of dollars in business due to unchecked file access errors. Taking the time to learn safe file handling practices can prevent these errors from occurring in your own projects.
The is_readable function accepts a single parameter, the name of the file to check. It returns True if the file is readable by PHP, and False otherwise. The result is cached, and can be cleared with clearstatcache().