Return to Snippet

Revision: 64097
at July 4, 2013 01:22 by rickygri

Initial Code

function exists($url) {
    // Get file contents limited to one character as
    // the whole file isn't needed, only the response
    // header
    file_get_contents($url, NULL, NULL, 0, 1);
    // Parse response
    $response = $http_response_header[0];
    // Split response to isolate reponse number
    $split = explode(" ", $response);
    // Return true if 200 header response received
    if ($split[1] == "200") return true;
    // Return false if 403 (not found) received
    else if ($split[1] == "403") return false;
    // Any other response return false
    else return false;

// Example usage - Check to see if image exists using img src url

//$url = "";
$url = "";
if (exists($url)) echo "image exists";
else echo "image does not exist";


Initial URL

Initial Description
This function can be used to check if a file exists using the HTTP header response. Example use, when looping through cached JSON objects (instagram API, etc.)

Initial Title
If file exists function - server-side

Initial Tags
php, file

Initial Language