I want to check the url
http://example.com/file.txt
exist or not in php. How can I do it?
Asked
Active
Viewed 7,037 times
4

Mohammed H
- 6,880
- 16
- 81
- 127
-
Possible duplicate of http://stackoverflow.com/questions/2280394/check-if-an-url-exists-in-php – swapnesh Nov 05 '12 at 05:33
5 Answers
4
if(! @ file_get_contents('http://www.domain.com/file.txt')){
echo 'path doesn't exist';
}
This is the easiest way to do it. If you are unfamiliar with the @
, that will instruct the function to return false if it would have otherwise thrown an error

Landon
- 4,088
- 3
- 28
- 42
-
OT: is there any general consensus over whether `@!` or `!@` is preferred? I use the former, and feel weird around the latter for some reason--maybe the `@` *after* an operator feels like it shouldn't work, even though it obviously should. – Tortoise Nov 05 '12 at 05:39
-
Fix the syntax error. and check the correct type of return. if(@file_get_contents('http://localhost/mh/t.php')===false){ echo 'path doesnt exist'; } – Mohammed H Nov 05 '12 at 05:47
-
Be sure you enable file_get_contents for other domains. That is done in php.ini. – ioan Nov 05 '12 at 05:48
-
@habeebperwad if you use an `@` and it fails, it will return `0`, not `false` - you can testing it with `if` as it is falsey, or `===0` but not `===false`. – doublesharp Nov 05 '12 at 05:52
-
"PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.". @ won't return any value, Right? The above code will failif the url returns a file with no content. – Mohammed H Nov 05 '12 at 07:54
3
The would use the PHP curl extension:
$ch = curl_init(); // set up curl
curl_setopt( $ch, CURLOPT_URL, $url ); // the url to request
if ( false===( $response = curl_exec( $ch ) ) ){ // fetch remote contents
$error = curl_error( $ch );
// doesn't exist
}
curl_close( $ch ); // close the resource

doublesharp
- 26,888
- 6
- 52
- 73
-
Yep. If file_exists doesn't work, this is the most canonically correct solution, though Landon's would also work. – Tortoise Nov 05 '12 at 05:38
0
$filename="http://example.com/file.txt";
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
OR
if (fopen($filename, "r"))
{
echo "File Exists";
}
else
{
echo "Can't Connect to File";
}

Soojoo
- 2,146
- 1
- 30
- 56
0
Try this function on Ping site and return result in PHP.
function urlExists($url=NULL)
{
if($url == NULL) return false;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if($httpcode>=200 && $httpcode<300){
return true;
} else {
return false;
}
}
0
I am agree with the response, I got success by doing this
$url = "http://example.com/file.txt";
if(! @(file_get_contents($url))){
return false;
}
$content = file_get_contents($url);
return $content;
You can follow the code to check the file exist at location or not.

Vinay Sikarwar
- 456
- 7
- 21