file_exists does not work

This is a discussion on file_exists does not work within the PHP Language forums, part of the PHP Programming Forums category; This is my code: if (file_exists($Fname)) { echo "<td>$Fname exists</td>"; } else { echo &...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-13-2007
Bob Sanderson
 
Posts: n/a
Default file_exists does not work

This is my code:

if (file_exists($Fname)) {
echo "<td>$Fname exists</td>";
} else {
echo "<td>$Fname does not exist</td>";
}

$Fname is the full path to the file I'm trying to verify. When I run the
script, I get the following output:

http://server1:8080/images/dwg_images/5005/5005001_.pdf does not exist

If I cut-and-paste the output path, the file is displayed, confirming that
the file does exist.

Am I doing something wrong? Any help will be greatly appreciated.
Reply With Quote
  #2 (permalink)  
Old 08-13-2007
Rik
 
Posts: n/a
Default Re: file_exists does not work

On Mon, 13 Aug 2007 14:36:19 +0200, Bob Sanderson
<news@LUVSPAMbobsanderson.com> wrote:

> This is my code:
>
> if (file_exists($Fname))


> http://server1:8080/images/dwg_images/5005/5005001_.pdf does not
> exist



From the manual:

File_exists:
<http://nl3.php.net/file_exists>:
"As of PHP 5.0.0 this function can also be used with some URL wrappers.
Refer to Appendix O, List of Supported Protocols/Wrappers for a listing of
which wrappers support stat() family of functionality."

HTTP wrapper:
<http://nl3.php.net/manual/en/wrappers.http.php>
Table O.2. Wrapper SummaryAttribute Supported
Restricted by allow_url_fopen Yes
Allows Reading Yes
Allows Writing No
Allows Appending No
Allows Simultaneous Reading and Writing N/A
Supports stat() No <---------------------------
Supports unlink() No
Supports rename() No
Supports mkdir() No
Supports rmdir() No

Ergo: file_exists does not play nice with http:// wrappers. If you want to
verify wether an URL exists, use fsockopen()(HEAD request), or CURL.
--
Rik Wasmus
Reply With Quote
  #3 (permalink)  
Old 08-13-2007
Bob Sanderson
 
Posts: n/a
Default Re: file_exists does not work

Rik <luiheidsgoeroe@hotmail.com> wrote in
news:op.twzz8gznqnv3q9@metallium:

> On Mon, 13 Aug 2007 14:36:19 +0200, Bob Sanderson
> <news@LUVSPAMbobsanderson.com> wrote:
>
>> This is my code:
>>
>> if (file_exists($Fname))

>
>> http://server1:8080/images/dwg_images/5005/5005001_.pdf does not
>> exist

>
>
> From the manual:
>
> File_exists:
> <http://nl3.php.net/file_exists>:
> "As of PHP 5.0.0 this function can also be used with some URL
> wrappers. Refer to Appendix O, List of Supported Protocols/Wrappers
> for a listing of which wrappers support stat() family of
> functionality."
>
> HTTP wrapper:
> <http://nl3.php.net/manual/en/wrappers.http.php>
> Table O.2. Wrapper SummaryAttribute Supported
> Restricted by allow_url_fopen Yes
> Allows Reading Yes
> Allows Writing No
> Allows Appending No
> Allows Simultaneous Reading and Writing N/A
> Supports stat() No <---------------------------
> Supports unlink() No
> Supports rename() No
> Supports mkdir() No
> Supports rmdir() No
>
> Ergo: file_exists does not play nice with http:// wrappers. If you
> want to verify wether an URL exists, use fsockopen()(HEAD request),
> or CURL.


Thanks for the reply but I'm a beginner with PHP and frankly, don't
understand any of what you said. Is there another, simple way to
determine if a file exists?

Reply With Quote
  #4 (permalink)  
Old 08-13-2007
Álvaro G. Vicario
 
Posts: n/a
Default Re: file_exists does not work

Bob Sanderson escribió:
> Thanks for the reply but I'm a beginner with PHP and frankly, don't
> understand any of what you said. Is there another, simple way to
> determine if a file exists?


You're trying to find out whether a URL points to a valid resource
without actually fetching the file. Are you sure that's what you really
need? If you just need to get a file from you own server you don't need
to play with HTTP: feed file_exists() with a file system path:

$file=$_SERVER['DOCUMENT_ROOT'].'/images/dwg_images/5005/5005001_.pdf';

if( file_exists($file) ){
// Whatever
}



--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor austrohúngaro: http://www.demogracia.com
--
Reply With Quote
  #5 (permalink)  
Old 08-13-2007
Krustov
 
Posts: n/a
Default Re: file_exists does not work

<comp.lang.php>
<Bob Sanderson>
<Mon, 13 Aug 2007 12:36:19 GMT>
<Xns998B5789B4327centroidincearthlink@69.28.186.15 8>

> This is my code:
>
> if (file_exists($Fname)) {
> echo "<td>$Fname exists</td>";
> } else {
> echo "<td>$Fname does not exist</td>";
> }
>
> $Fname is the full path to the file I'm trying to verify. When I run the
> script, I get the following output:
>
> http://server1:8080/images/dwg_images/5005/5005001_.pdf does not exist
>
> If I cut-and-paste the output path, the file is displayed, confirming that
> the file does exist.
>


As another user its best if you check the actual folder path rather than
a http url .

You can also write it like the below if you dont want to use else .


$pass=1;

$tempname="images/5005/5005001.pdf";

if (!file_exists($tempname)) {$pass=0;}

if ($pass==0) {print "<td>$tempname FILE NOT FOUND</td>";}

if ($pass==1) {print "<td>$tempname OK</td>";}

Reply With Quote
  #6 (permalink)  
Old 08-13-2007
Rik
 
Posts: n/a
Default Re: file_exists does not work

On Mon, 13 Aug 2007 15:34:27 +0200, Krustov <me@privacy.net> wrote:
> $pass=1;
> if (!file_exists($tempname)) {$pass=0;}
> if ($pass==0) //
> if ($pass==1) //


Euhm,
if(file_exists()){
//something
} else {
//something else
}

Keep it legible for other coders, limit the amount of random variables
popping up in your script if you don't need them, and avoid
double/repeated comparisons...
--
Rik Wasmus
Reply With Quote
  #7 (permalink)  
Old 08-13-2007
Rik
 
Posts: n/a
Default Re: file_exists does not work

On Mon, 13 Aug 2007 14:59:07 +0200, Bob Sanderson
<news@LUVSPAMbobsanderson.com> wrote:
> Thanks for the reply but I'm a beginner with PHP and frankly, don't
> understand any of what you said. Is there another, simple way to
> determine if a file exists?


A 'file' is not the proper term for something accessed by the HTTP
protocol. As others have indicated, if the file is on the same server, you
should use the local filesystem instead of the long way around by http.

If it is on another server however, look at the user contributed notes at
<http://www.php.net/file_exists>, several options for determining wether
an URL can be 'found' are given there.
--
Rik Wasmus
Reply With Quote
  #8 (permalink)  
Old 08-13-2007
Krustov
 
Posts: n/a
Default Re: file_exists does not work

<comp.lang.php>
<Rik>
<Mon, 13 Aug 2007 15:38:56 +0200>
<op.twz2u6xyqnv3q9@metallium>

> > $pass=1;
> > if (!file_exists($tempname)) {$pass=0;}
> > if ($pass==0) //
> > if ($pass==1) //

>
> Euhm,
> if(file_exists()){
> //something
> } else {
> //something else
> }
>
> Keep it legible for other coders, limit the amount of random variables
> popping up in your script if you don't need them, and avoid
> double/repeated comparisons...
>


Up your arse .

I dont recall asking you for your zen like wisdom on how i should or
shouldnt write code .
Reply With Quote
  #9 (permalink)  
Old 08-13-2007
Rik
 
Posts: n/a
Default [OT: conditional statements] Re: file_exists does not work

On Mon, 13 Aug 2007 15:51:28 +0200, Krustov <me@privacy.net> wrote:

> <comp.lang.php>
> <Rik>
> <Mon, 13 Aug 2007 15:38:56 +0200>
> <op.twz2u6xyqnv3q9@metallium>
>
>> > $pass=1;
>> > if (!file_exists($tempname)) {$pass=0;}
>> > if ($pass==0) //
>> > if ($pass==1) //

>>
>> Euhm,
>> if(file_exists()){
>> //something
>> } else {
>> //something else
>> }
>>
>> Keep it legible for other coders, limit the amount of random variables
>> popping up in your script if you don't need them, and avoid
>> double/repeated comparisons...
>>

>
> Up your arse .


Why, thank you. That is exactly what you say to other coders that might
work on the same project with this kind of code.

> I dont recall asking you for your zen like wisdom on how i should or
> shouldnt write code.


Neither did the OP ask for lousy coding practises. Several people allready
told him he should try the local filesystem, the only thing you added was
cumbersome code most of us would be ashamed to have in our scripts. What
was _your_ reasoning for posting this enlightened code?

--
Rik Wasmus
Reply With Quote
  #10 (permalink)  
Old 08-13-2007
Bob Sanderson
 
Posts: n/a
Default Re: file_exists does not work

Rik <luiheidsgoeroe@hotmail.com> wrote in news:op.twz2u6xyqnv3q9@metallium:

> On Mon, 13 Aug 2007 15:34:27 +0200, Krustov <me@privacy.net> wrote:
>> $pass=1;
>> if (!file_exists($tempname)) {$pass=0;}
>> if ($pass==0) //
>> if ($pass==1) //

>
> Euhm,
> if(file_exists()){
> //something
> } else {
> //something else
> }


That did it. Thanks to all.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:18 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0