Jump to content

Checking wither files are accessable with is_readable and file_exists()


Xeoncross

Recommended Posts

 

I have a design question. Has anyone ever seen a difference between the file_exists() and is_readable() function? php.net states that:

 

 

File_exists()

 

Returns TRUE if the file or directory specified by filename  exists; FALSE otherwise. The check is done using the real UID/GID instead of the effective one.

 

Note: This function will return FALSE for symlinks pointing to non-existing files.

 

Warning

This function returns FALSE for files inaccessible due to safe mode restrictions. However these files still can be included if they are located in safe_mode_include_dir.

 

is_readable

Returns TRUE if the file or directory specified by filename  exists and is readable, FALSE otherwise. Keep in mind that PHP may be accessing the file as the user id that the web server runs as (often 'nobody'). Safe mode limitations are not taken into account before PHP 5.1.5.

 

Note: The check is done using the real UID/GID instead of the effective one.

 

You see, I want to read files that I am not sure exist. I was going to keep three speporate lists of them - but since there will never be more than 50 altogether I thought that just checking for a files existence woudld be better.

 

ie:
plugin
    ->file_1
    ->file_2
    ->file_3
plugin_2
    ->file_2
    ->file_3
plugin 3
    ->file_1
    ->file_3

 

Anyway, is there ever a time when file_exists() will return FALSE when is_readable() returns TRUE? Or vise-versa? My benchmarks show the speed is about the same.

 

Link to comment
Share on other sites

I don't believe file_exists() would in any case return false when is_readable() returns true. visa-versa = 'yes' because is_readable() wants to know not only that the file exists, but that it's readable as well.

 

True but I am mostly wondering about odd things that I might miss just reading the function info.

 

is_readable() checks whether you can do file_get_contents() or similar calls, no more, no less. If the location given returns a 500 or 403 error, you can still read() that (you'll simply get the error page), but it's still read()able. Using is_readable to check the validity of a URL is simply the wrong function. - jo at durchholz dot org

 

Of course I would never use anything but cURL for HTTP requests...

 

Link to comment
Share on other sites

Anyway, is there ever a time when file_exists() will return FALSE when is_readable() returns TRUE?

 

No.

 

Or vise-versa?

 

Yes. Try to chmod a file 0 and check. The file does exist but it's not readable. This only works if safe mode is off, as the manual states.

 

Note: You might not want to do that unless you're root. Otherwise you can't get it deleted or change it's permissions again.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.