FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » test for file existance
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: test for file existance [message #182854 is a reply to message #182853] Sat, 21 September 2013 16:45 Go to previous messageGo to previous message
Lew Pitcher is currently offline  Lew Pitcher
Messages: 60
Registered: April 2013
Karma:
Member
On Saturday 21 September 2013 11:55, in comp.lang.php, noreply(at)example(dot)com
wrote:

> On Sat, 21 Sep 2013 10:16:02 -0400, richard wrote:
>
>> What would be a good way to test to see if the file actually esists?
>>
>> In my database I may have a file name listed, but don't have the actual
>> file.
>> So if you don't have the file, then why reference it?
>> For simplicity. When I enter one part of the pair, the other part simply
>> gets a "b" added to the name.
>> e.g 100 and 100b.
>>
>> Because there is no file to work with, I want to be able to post a notice
>> saying "not available" or what ever.
>
> Using their own example:
> <?php
> $filename = '/path/to/foo.txt';
>
> if (file_exists($filename)) {
> echo "The file $filename exists";
> } else {
> echo "The file $filename does not exist";
> }
> ?>
>
> Having tested this, it appears the response is wrong.
> I am told the file does not exist even if it does.

Does your usage fall into one of the caveats (see
http://ca1.php.net/manual/en/function.file-exists.php) ?

For instance:
- are you testing a symlink that links to a non-existant file?
(if the filename is a symlink to non-existant file, the function will
return FALSE)

- is the path readable by the "real uid and gid" of the webserver process?
(if the real uid/gid doesn't have "execute" access to each and every
directory in the path to the file, the function will return FALSE)

- have you previously tested and found that the file does not exist?
(the function caches results; the results of previous tests of a given
filename will be returned, instead of the current state - use the
clearstatcache() function before calling file_exists() )

> is there something else I could use?

The stat() function would do, in a pinch.

However, there is another problem with stat()-like functions (including the
file_exists() function): they can introduce race conditions into your code.

Imagine that, between the time you issue the file_exists() call and the time
you use the results, an outside operation deletes the file. Now, you report
that the file exists, even though it doesn't.

Similarly, if some operation creates the file during that time, then you
report that the file doesn't exist, even though it does.

One way to mitigate the race condition is to actually fopen() the file,
instead of checking the file stat with file_exists() or stat(). If fopen()
succeeds, then the file exists, and will exist until you fclose() it. No
intervening external delete will affect the access to the file during your
use of it. OTOH, if fopen() fails, then the file does not exist (but may be
created before you report the non-existance).

HTH
--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: ann: PDF report writer
Next Topic: Setting & displaying Variables
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Tue Nov 26 15:21:17 GMT 2024

Total time taken to generate the page: 0.04271 seconds