0

I'm trying to tidy up my error logs. When I use fopen to retrieve a file behind HTTP auth and it fails, I get the error message:

fopen: failed to open stream: No such file or directory

Is there a check I can do before using fopen to see if this would fail?

Here's an example URL: http://12345:12345@datatransfer.cj.com/datatransfer/files/products.zip

Sean H
  • 1,045
  • 1
  • 14
  • 32

1 Answers1

0

I did this by checking the file headers for authentication:

$file_headers = get_headers($url);
if (!$file_headers) return "No headers found";
else if (strpos($file_headers[0], '401 Unauthorized') > -1) return "Authorization required";        
else if (strpos($file_headers[0], '404 Not Found') > -1) return "404 Not Found";
else {
    // now safe to use fopen...
}
Sean H
  • 1,045
  • 1
  • 14
  • 32