Log in

View Full Version : file_exist alternative (returning false)



JShor
07-05-2007, 04:39 PM
Hello, I am trying to create a script that will detect if a URL exists or not. I was using the function, file_exists, but every single time it returns false, regardless if the URL exists or not. Is there any alternative to this? :confused: :confused:

THe code is as follows:


<?php

$file = 'http://www.google.com/'

if (file_exists($file)) {
echo ('This URL exists!');
}
else {
echo ('This URL is unavailable');
}
?>

mwinter
07-05-2007, 04:57 PM
Hello, I am trying to create a script that will detect if a URL exists or not. I was using the function, file_exists, but every single time it returns false, regardless if the URL exists or not.

It will: the file_exists function requires stat family support to work with URI wrappers, but stat family support isn't provided for the HTTP and HTTPS protocols. The PHP manual would have told you this.



Is there any alternative to this?

Depends on what URIs will be used and what you think constitutes existence. If only HTTP URIs are involved, you can send a HTTP request to the host and check the response, though there are a lot of possibilities to consider than just a 200 status.

djr33
07-05-2007, 07:12 PM
You're missing a semicolon -- ; on the third line.

This may have to do with remote files. There should be a setting you can turn on to allow opening remote files.

One alternative is to try include('some_url'), and use an output buffer. This is a lot of extra work, though.

thetestingsite
07-08-2007, 12:23 AM
Another thing you could try is using fsockopen to connect to the certain site you are looking for, then see if you get a respond from there. Beware when doing this, though, as it could cause the script to lag out.

Hope this helps nonetheless.

blm126
07-08-2007, 12:29 AM
There are lots of different ways to do this. The best way will depend on what you are going to do once you find if it exists. Here's one solution.


$url = 'http://www.google.com';
$handle = fopen($url,'r');
if($handle !== false){
echo 'Exists';
}
else{
echo "Doesn't";
}

JShor
07-09-2007, 03:46 PM
gotcha! it works blm, thx!