Images can be partially cached by a browser?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zod2009
Forum Newbie
Posts: 2
Joined: Wed Feb 04, 2009 4:04 pm

Images can be partially cached by a browser?

Post by zod2009 »

Hi everyone. I've been struggling with this for a while. Hope someone can help.

Before you read this please note: I DO NOT want to disable browser cacheing. I need it to reduce bandwidth costs.

I wrote some PHP code to output an image which will be cached by the browser (see code below). This is pretty common and I've seen examples just like this all over the place. Everything works fine. But....

What if there is an error half way through outputting the image? Half of the image gets cached!!! The next time you load the page only half the image shows up! This can happen if apache gets restarted, if the server is rebooted, if theres a power outage, if there is a bug in the code, etc.

It may not be too bad for images, but I was thinking of doing the same thing with flv files, which take longer to download (therefore more room for something to go wrong).

Is there a way to tell the browser to only cache the image after the WHOLE image gets downloaded? I there some other solution?

I searched for days and I'm surprised that I couldn't find a single article about this.


Here is a simplified version of my code. I know this code is not secure but its just an example.

Code: Select all

<?php
    // Figure out which image to send them
    $image = $_GET['image'];
    
    $fsize=filesize("C:/path/to/image/$image");
    $fh = fopen("C:/path/to/image/$image", "rb");
    
    header("Content-type: image/jpeg");
    header('Content-length: '. $fsize);
    header("Cache-Control: must-revalidate");
    //Set to expire in 3 days
    $offset = 60 * 60 * 24 * 3;
    $ExpStr = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
    header($ExpStr);
    
    while (!feof($fh)) 
    {
        $buffer=fread($fh, 1024);
        
                //what is something goes wrong here?
        if ($buffer===FALSE)    
        {
            fclose($fh);
            exit(1);
    
        }
        echo $buffer;
    }
    
    exit(0);
?>
Can anyone help?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Images can be partially cached by a browser?

Post by pickle »

Are you positive only part of the image gets cached? I would think browsers would be smart enough to not cache a file unless they have all of it.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
zod2009
Forum Newbie
Posts: 2
Joined: Wed Feb 04, 2009 4:04 pm

Re: Images can be partially cached by a browser?

Post by zod2009 »

Yeah, only part of it gets cached. But I think I found a solution. I added a flush statement after
the headers, and another after the echo statment. I'm not really sure why but it works now.

I'm still having problems with flv files (which are loaded into a swf player). IE will still cache part of the flv on a failure. Firefox doesn't. Maybe its a flash issue and not PHP? Does anyone know?

Anyway, I'll have to look into more.
Still Learning
Forum Commoner
Posts: 25
Joined: Tue Jan 27, 2009 3:57 pm
Location: Philadelphia

Re: Images can be partially cached by a browser?

Post by Still Learning »

How many images are we talking about? If you're that worried about bandwidth costs maybe upload a bunch to http://imageshack.us and just hotlink them.

Save them on your computer or somewhere else of course because I imagine they have an expiration, but that'll get rid of all image bandwidth. As for FLV's.... do you have your own video player that you need to keep?

Why not upload some to youtube and then just embed them?

Those 2 things could save a bunch of bandwidth.
Post Reply