drisate Posted November 4, 2009 Share Posted November 4, 2009 Hey guys i need to teste if an image exists or not ... and my function returns true what ever i put in it ... function url_exists($url) { $hdrs = @get_headers($url); return is_array($hdrs) ? preg_match('/^HTTP\\/\\d+\\.\\d+\\s+2\\d\\d\\s+.*$/',$hdrs[0]) : false; } The function needs to be as faste as possible because it has to teste 300 jpg image on page load Link to comment https://forums.phpfreaks.com/topic/180308-function-url_existsurl/ Share on other sites More sharing options...
lemmin Posted November 4, 2009 Share Posted November 4, 2009 Just do : if (@get_headers($url)) return true; else return false; That is going to take a while because, no matter how you do it, you will have to wait for a timeout to determine if the url exists or not. Link to comment https://forums.phpfreaks.com/topic/180308-function-url_existsurl/#findComment-951154 Share on other sites More sharing options...
simshaun Posted November 4, 2009 Share Posted November 4, 2009 You can specify a timeout if you use cURL. See the first user-comment on this page. Link to comment https://forums.phpfreaks.com/topic/180308-function-url_existsurl/#findComment-951163 Share on other sites More sharing options...
lemmin Posted November 4, 2009 Share Posted November 4, 2009 Even so, you don't know how long a server is going to take to respond so setting a timeout too low could result in a false negative. Link to comment https://forums.phpfreaks.com/topic/180308-function-url_existsurl/#findComment-951166 Share on other sites More sharing options...
simshaun Posted November 4, 2009 Share Posted November 4, 2009 Yes, but you can adjust it to something you are comfortable with. It does not necessarily have to be low. Edit: To the OP, can you not test the images before page load? Link to comment https://forums.phpfreaks.com/topic/180308-function-url_existsurl/#findComment-951170 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.