if file exisits problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

if file exisits problem

Post 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?
rufee
Forum Newbie
Posts: 10
Joined: Wed Dec 09, 2009 10:23 am

Re: if file exisits problem

Post by rufee »

I think file_exists(); is for local files
try fopen();
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: if file exisits problem

Post 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";
}
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: if file exisits problem

Post 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]
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: if file exisits problem

Post 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";
}
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: if file exisits problem

Post by AbraCadaver »

I don't know. That should work fine, what's the problem?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
rufee
Forum Newbie
Posts: 10
Joined: Wed Dec 09, 2009 10:23 am

Re: if file exisits problem

Post by rufee »

Try this

Code: Select all

$fopen = fopen('http://'.$url.'/robots.txt', "r");
 
if ($fopen) {
$robots = "Yes";
} else {
$robots = "No";
}
 
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: if file exisits problem

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: if file exisits problem

Post 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?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: if file exisits problem

Post by pickle »

Are you sure url_wrappers are enabled?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: if file exisits problem

Post 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 )
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: if file exisits problem

Post 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.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: if file exisits problem

Post 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?
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: if file exisits problem

Post by someguyhere »

Solved

Code: Select all

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