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 &...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
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? |
|
|||
|
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 -- |
|
|||
|
<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>";} |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
<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 . |
|
|||
|
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 |
|
|||
|
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. |