A function to see if a file exists with curl.

/ Published in: PHP
Save to your folder(s)

This function will use curl to see if a file exists at the location it's provided. It accepts $_path as a parameter.

Copy this code and paste it in your HTML
  1. function file_alive($_path) {
  2. $handle = curl_init($_path);
  3. if (false === $handle) {return false;}
  4. curl_setopt($handle, CURLOPT_HEADER, false);
  5. curl_setopt($handle, CURLOPT_FAILONERROR, true);
  6. curl_setopt($handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20080623 Firefox/") ); // request as if Firefox because some sites request a valid header from you
  7. curl_setopt($handle, CURLOPT_NOBODY, true);
  8. curl_setopt($handle, CURLOPT_RETURNTRANSFER, false);
  9. $alive = curl_exec($handle);
  10. curl_close($handle);
  11. return $alive;
  12. }

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.