Last active
May 31, 2017 07:21
-
-
Save amitmerchant1990/6eed95fa9568edd1b332fabc9b3f87bc to your computer and use it in GitHub Desktop.
PHP : Check it remote file exists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// E.g. checkRemoteFileExists('https://avatars0.githubusercontent.com/u/3647841?v=3&s=460') == true | |
function checkRemoteFileExists($url){ | |
$curl = curl_init($url); | |
//don't fetch the actual page, you only want to check the connection is ok | |
curl_setopt($curl, CURLOPT_NOBODY, true); | |
//do request | |
$result = curl_exec($curl); | |
$ret = false; | |
//if request did not fail | |
if ($result !== false) { | |
//if request was ok, check response code | |
$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
if ($statusCode == 200) { | |
$ret = true; | |
} | |
} | |
curl_close($curl); | |
return $ret; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment