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
Switch to threaded view of this topic Create a new topic Submit Reply
test for file existance [message #182851] Sat, 21 September 2013 14:16 Go to next message
richard is currently offline  richard   
Messages: 213
Registered: June 2013
Karma: 0
Senior Member
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.
Re: test for file existance [message #182852 is a reply to message #182851] Sat, 21 September 2013 14:35 Go to previous messageGo to next message
richard is currently offline  richard   
Messages: 213
Registered: June 2013
Karma: 0
Senior Member
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.

well duhh.
If I use file_exists(), it does what I want just fine.
Re: test for file existance [message #182853 is a reply to message #182851] Sat, 21 September 2013 15:55 Go to previous messageGo to next message
richard is currently offline  richard   
Messages: 213
Registered: June 2013
Karma: 0
Senior Member
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.
is there something else I could use?
Re: test for file existance [message #182854 is a reply to message #182853] Sat, 21 September 2013 16:45 Go to previous messageGo to next message
Lew Pitcher is currently offline  Lew Pitcher
Messages: 60
Registered: April 2013
Karma: 0
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
Re: test for file existance [message #182855 is a reply to message #182853] Sat, 21 September 2013 16:46 Go to previous messageGo to next message
Norman Peelman is currently offline  Norman Peelman
Messages: 126
Registered: September 2010
Karma: 0
Senior Member
On 09/21/2013 11:55 AM, richard 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.
> is there something else I could use?
>

Works here... check your path. And provide your code snippet with output.

--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
Re: test for file existance [message #182856 is a reply to message #182853] Sat, 21 September 2013 17:15 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
On 21/09/13 16:55, richard 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.
> is there something else I could use?
>
angle grinders are sutaible for use where files dont exist.

--
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.
Re: test for file existance [message #182857 is a reply to message #182856] Sat, 21 September 2013 18:43 Go to previous messageGo to next message
Lew Pitcher is currently offline  Lew Pitcher
Messages: 60
Registered: April 2013
Karma: 0
Member
On Saturday 21 September 2013 13:15, in comp.lang.php, tnp(at)invalid(dot)invalid
wrote:

> On 21/09/13 16:55, richard 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.
>> is there something else I could use?
>>
> angle grinders are sutaible for use where files dont exist.

Can you use an angle grinder to create a file?


--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request
Re: test for file existance [message #182858 is a reply to message #182857] Sat, 21 September 2013 19:00 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
On 21/09/13 19:43, Lew Pitcher wrote:
> On Saturday 21 September 2013 13:15, in comp.lang.php, tnp(at)invalid(dot)invalid
> wrote:
>
>> On 21/09/13 16:55, richard 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.
>>> is there something else I could use?
>>>
>> angle grinders are sutaible for use where files dont exist.
>
> Can you use an angle grinder to create a file?
>
>
I would say it's entirely possible, yes.


--
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to
lead are elected by the least capable of producing, and where the
members of society least likely to sustain themselves or succeed, are
rewarded with goods and services paid for by the confiscated wealth of a
diminishing number of producers.
Re: test for file existance [message #182863 is a reply to message #182857] Sat, 21 September 2013 21:28 Go to previous message
Richard Yates is currently offline  Richard Yates
Messages: 86
Registered: September 2013
Karma: 0
Member
On Sat, 21 Sep 2013 14:43:30 -0400, Lew Pitcher
<lew(dot)pitcher(at)digitalfreehold(dot)ca> wrote:

> On Saturday 21 September 2013 13:15, in comp.lang.php, tnp(at)invalid(dot)invalid
> wrote:
>
>> On 21/09/13 16:55, richard 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.
>>> is there something else I could use?
>>>
>> angle grinders are sutaible for use where files dont exist.
>
> Can you use an angle grinder to create a file?

That would be Object Oriented style, I believe.
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: ann: PDF report writer
Next Topic: Setting & displaying Variables
Goto Forum:
  

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

Current Time: Thu Dec 05 02:24:21 GMT 2024

Total time taken to generate the page: 0.02615 seconds