Page 1 of 1

if file exisits problem

Posted: Wed Dec 09, 2009 11:35 am
by someguyhere
I have a bit of code (part of a larger script) that checks to see if a robots.txt file exists, but it isn't working.

Code: Select all

$filename = 'http://'.$url.'/robots.txt';
 
if (file_exists($filename)) {
    $robots = "Yes";
} else {
    $robots = "No";
}
$url is declared earlier - an example would be msn.com

Is there something wrong w/ my code or am I using the wrong approach?

Re: if file exisits problem

Posted: Wed Dec 09, 2009 11:38 am
by rufee
I think file_exists(); is for local files
try fopen();

Re: if file exisits problem

Posted: Wed Dec 09, 2009 12:08 pm
by someguyhere
Hmmm...ok. I'm running into a problem with that...I'm probably using the if/then incorrectly here:

Code: Select all

$robots = fopen('http://'.$url.'/robots.txt', "r");
 
if ($robots = null) {
    $robots = "No";
} else {
    $robots = "Yes";
}

Re: if file exisits problem

Posted: Wed Dec 09, 2009 12:17 pm
by AbraCadaver
fopen() doesn't return null. If it fails it returns false. Or try this:

Code: Select all

$headers = get_headers('http://'.$url.'/robots.txt');
print_r($headers);
// look at $headers[0]

Re: if file exisits problem

Posted: Wed Dec 09, 2009 12:30 pm
by someguyhere
AbraCadaver wrote:fopen() doesn't return null. If it fails it returns false.
Ok, I'm a PHP noobie...what am I doing wrong here:

Code: Select all

$robots = fopen('http://'.$url.'/robots.txt', "r");
 
if ($robots == FALSE) {
    $robots = "No";
} else {
    $robots = "Yes";
}

Re: if file exisits problem

Posted: Wed Dec 09, 2009 12:55 pm
by AbraCadaver
I don't know. That should work fine, what's the problem?

Re: if file exisits problem

Posted: Wed Dec 09, 2009 1:00 pm
by rufee
Try this

Code: Select all

$fopen = fopen('http://'.$url.'/robots.txt', "r");
 
if ($fopen) {
$robots = "Yes";
} else {
$robots = "No";
}
 

Re: if file exisits problem

Posted: Wed Dec 09, 2009 1:09 pm
by someguyhere
AbraCadaver wrote:I don't know. That should work fine, what's the problem?
No matter what I do, it keeps returing "Yes" whether the robots.txt file exisits or not.

Re: if file exisits problem

Posted: Wed Dec 09, 2009 1:17 pm
by AbraCadaver
AbraCadaver wrote:fopen() doesn't return null. If it fails it returns false. Or try this:

Code: Select all

$headers = get_headers('http://'.$url.'/robots.txt');
print_r($headers);
// look at $headers[0]
What does this show?

Re: if file exisits problem

Posted: Wed Dec 09, 2009 1:25 pm
by pickle
Are you sure url_wrappers are enabled?

Re: if file exisits problem

Posted: Wed Dec 09, 2009 1:30 pm
by someguyhere
AbraCadaver wrote:What does this show?
Array ( [0] => HTTP/1.1 200 OK [1] => Date: Wed, 09 Dec 2009 19:29:12 GMT [2] => Server: Apache/2.2.14 (Unix) mod_ssl/2.2.14 OpenSSL/0.9.8l DAV/2 mod_auth_passthrough/2.1 FrontPage/5.0.2.2635 [3] => Last-Modified: Wed, 27 May 2009 19:04:43 GMT [4] => ETag: "53d0a17-71-46ae9841908c0" [5] => Accept-Ranges: bytes [6] => Content-Length: 113 [7] => Connection: close [8] => Content-Type: text/plain )

Re: if file exisits problem

Posted: Wed Dec 09, 2009 1:44 pm
by AbraCadaver
So that is telling us that the server returned something, 200 OK. Whether it was a robots.txt file or not we can't tell. If the server does not return a 404 then fopen() thinks it opened it.

Re: if file exisits problem

Posted: Wed Dec 09, 2009 2:14 pm
by someguyhere
Ok, I found a better approach here:

Code: Select all

$robotsurl = "http://".$url."/robots.txt";
 
if (fopen($robotsurl, "r")) {
$robots = "Yes";
} else {
$robots = "No";
}
But if the file doesn't exist, I get an error message:

Warning: fopen() [function.fopen]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/wildfir1/public_html/AREA-52/DS/db.php on line 200

Warning: fopen(http://http://www.heidelbergpr.com/robots.txt) [function.fopen]: failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/wildfir1/public_html/AREA-52/DS/db.php on line 200

How can I stop that from happening?

Re: if file exisits problem

Posted: Wed Dec 09, 2009 2:20 pm
by someguyhere
Solved

Code: Select all

$robotsurl = "http://".$url."/robots.txt";
 
if (@fopen($robotsurl, "r")) {
$robots = "Yes";
} else {
$robots = "No";
}