Xeoncross Posted June 14, 2008 Share Posted June 14, 2008 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. Quote Link to comment Share on other sites More sharing options...
hansford Posted June 14, 2008 Share Posted June 14, 2008 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. Quote Link to comment Share on other sites More sharing options...
Xeoncross Posted June 14, 2008 Author Share Posted June 14, 2008 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... Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted June 15, 2008 Share Posted June 15, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.