/ Published in: PHP

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.)
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php function exists($url) { // Get file contents limited to one character as // the whole file isn't needed, only the response // header // Parse response $response = $http_response_header[0]; // Split response to isolate reponse number // 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 = "http://distilleryimage8.s3.amazonaws.com/4a2813d8785e11e2893322000a1f9ca0_7asd.jpg"; $url = "http://distilleryimage8.s3.amazonaws.com/4a2813d8785e11e2893322000a1f9ca0_7.jpg/"; if (exists($url)) echo "image exists"; else echo "image does not exist"; ?>
Comments
