Well, i know that there 100 methods to do that but the thing is that I want to check existence of files which might be as big as 100 MBS. So requesting the whole file and checking existence will be huge waste of bandwidth and time. Any way to solve my problem.?
In situations where you need more control (such as having to login first, having to specify the referer, etc) - I'd recommend using cURL. Basically, you use the "HEAD" request (rather than GET/POST) to retrieve the response headers for the URL in question. So with cURL, you could use something like this: function response_code($url) { $ch = curl_init($url); $c = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // Return content, rather than printing it out $c = curl_setopt($ch, CURLOPT_NOBODY, 1); // Use HTTP "HEAD" METHOD $c = curl_setopt($ch, CURLOPT_HEADER, 1); // Include response headers $x = curl_exec($ch); // Execute the request $s = curl_getinfo($ch, CURLINFO_HTTP_CODE); // Fetch the server response status $c = curl_close($ch); // Close the cURL handle return($s); // Return the status to the caller } PHP: If you need the function to follow redirects (such as status code 301), then add this to the function before calling curl_exec(): $c = curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); PHP: Some example code using the function: $google_example1 = "http://www.google.com/fake"; $google_example2 = "http://www.google.com/search?q=real"; print("$google_example1: ".response_code($google_example1)."\r\n"); print("$google_example2: ".response_code($google_example2)."\r\n"); PHP: And the result: C:\>php example.php http://www.google.com/fake: 404 http://www.google.com/search?q=real: 200
If the file is on the local machine use file_exists() Edit: "As of PHP 5.0.0, this function can also be used with some URL wrappers." so you could use that for a URL as well potentially.
I didn't go over the code with a fine tooth comb, but papsyface is thinking the same thing I am with using cURL and a HEAD request.
I didnt make a test but I think you can you file_get_contents() <?php $url = "http://asdf123456.com"; if(!file_get_contents($url)) { echo "link is not exist"; } else echo "yes it is exist"; ?>