How do I check if a URI exists with PHP?

I guess it will return an error code and I can check it before I use file_get_contents, because if I use file_get_contents on a link that doesn't exist, it gives me an error.

Comments

how does this question not answer itself?

Written by Pointy

it doesnt return me an error code i can do a if-statement with. it gives me an ERROR PAGE.

Written by weng

Accepted Answer

try the function array get_headers($url, [, int $format = 0 ]), it should return false on failure - otherwise, you can assume the uri exists, since the web server provided you with header info.

I hope the function uses an HTTP HEAD request as opposed to a GET, which should result in a lot less traffic than in the fopen solutions above.

Written by Daren Thomas
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki