Check an Image on a Remote Server
This is a common task. You’d like to know whether the image on the remote server exists. Zend Framework gives the answer of this question and in particular this can be completed with Zend_Http_Client.
Content-Type and Content-Length
The little problem is that checking for content-type is not always correct, because it will return an image content type even when the image does not exists, so it’s better to check for content-length.
Code
The simple way to check this is like that:
$client = new Zend_Http_Client('http://remote-machine/image.jpg'); $response = $client->request('head'); if ( NULL == $response->getHeader('Content-Length') ) // do whatever if the image does not exists. |
In fact here, in this example I don’t check for GET request, because is way to slow than the HEAD method, but you should be aware of incorrect responses when using HEAD. In fact if you request the HEAD of a FLV video it will be returned text content-type, while using GET everything’s working fine but slow though.
nice example, stoimen, thanks