Skip to content

PHP Check If A Resource Pointed By Url Is Valid

PHP Check If A Resource Pointed By Url Is Valid

Sometimes we might have to check if a particular video or image exists and works, and to do so, some people might choose to download the entire resource file using something like file_get_contents which might not be the most effective and efficient way of doing so. A better way is to use curl and simply check the http status code returned.
* Check if a resource is valid (or exists)
* Works with pretty much anything - jpg, mp4 ...
* @url : the url must be a direct link (or hotlink) to the resource.
function is_resource_valid($resource_url)
    $resource_exists = false;
    $ch = curl_init($resource_url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    if($status_code == '200'){
        $resource_exists = true;
    return $resource_exists;
Enjoyed the content ? Share it with your friends !
Published inDevelopmentProgramming

Be First to Comment

Leave a Reply

Your email address will not be published.