Skip to content
WJunction - Webmaster Forum

Need faster function - help :(

Status
Not open for further replies.

7 comments

This is the function I use to return the filesize of a remote image.
PHP:
function ffilesize($file){
    $ch = curl_init($file);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, true);
    $data = curl_exec($ch);
    curl_close($ch);
    if ($data === false) 
      return false;
    if (preg_match('/Content-Length: (\d+)/', $data, $matches)) {
      return (float)$matches[1];
	  }
    elseif (preg_match('/content-length: (\d+)/', $data, $matches)) {
      return (float)$matches[1];
      }
}

I use it on lulzimg.com which is a very heavy traffic site with thousands of images uploaded everyday. And it IS very fast. If it's slow for you, maybe your server's on a congested network or the target urls are responding slow.
 
I have another question.
What will be the fastest way to check if remote image exist ?
Maybe i can use again the filesize ? 404 and the other errors have no filesize or it is in bytes?
 
What exactly are you trying to do. Where are the images and what are you trying to do to them. We may then be able to offer a better way of doing it.
 
Status
Not open for further replies.

About the author

Porsche_maniak
Established Member · Joined
Auto racing,Porsche,Web Development
283
Messages
0
Reactions
16
Points
Grow your business

Advertise on WJunction

Connect with an established international community of webmasters, hosting providers, developers and online entrepreneurs.

  • Premium & Premium+ Sticky
  • Dedicated section banners
  • Site-wide banner advertising
Explore advertising options
Back
Top Bottom