1

I need to determine if a remote file exists or not.

I have used curl, but it propably reads whole file because it's very slow.

Is there any fast method to "touch" a remote file (for example read only one byte of that file or return false when a 404 occurs)?

Also there could be a problem if tested domain does not exist or connection simply times out. So there has to be timeout interval set to lower than the usual value.

tohuwawohu
  • 13,268
  • 4
  • 42
  • 61
Kryštof Hilar
  • 609
  • 2
  • 10
  • 22
  • 1
    This has been asked and answered before : Here http://stackoverflow.com/questions/981954/how-can-one-check-to-see-if-a-remote-file-exists-using-php – Mob Nov 14 '11 at 18:41

2 Answers2

4

You have to send HEAD request, in this way only HTTP headers will be returned, and not the whole file

curl_setopt(CURLOPT_NOBODY, true);
Maxim Krizhanovsky
  • 26,265
  • 5
  • 59
  • 89
1

When you just want to check the existance of the file, you can use curl to make a HEAD Request instead of a GET. This way the webserver will only answer with the header information (including the 200/404/... status code) and not the entire file

klaustopher
  • 6,702
  • 1
  • 22
  • 25
  • ..and if status is 200 it's ok, in case of some 300s code, i will "redirect myself" to new one (for example google.com is moved).... Thank you ! You saved my time ;-) Thank you all – Kryštof Hilar Nov 14 '11 at 18:49