Real effective technique to refresh image after upload

images php coding issues or problems here.

Moderators: macek, egami, gesf

User avatar
gandahar
New php-forum User
New php-forum User
Posts: 112
Joined: Tue Mar 15, 2011 4:05 am

Real effective technique to refresh image after upload

Postby gandahar » Tue Mar 15, 2011 4:18 am

Hi !

When creating a web application with an upload image system, there is this problem with cache, you upload and replace an existing image without changing name, the browser still show the old image, you must hit F5 or refresh to force reload the image.

I've been looking for a simple way to fix this common problem.

I found the

Code: Select all

<?php echo '<img src="image.jpg?'.rnd(0,9999).'">'; ?>
technique but not good for me

there is another trick with the php function DATE, but not good for me

I found an effective solution (i think) using the filesize function, to get the image size in bytes :

Code: Select all

<?php
$the_photo='image.jpg';
$bytes_photo='?'.filesize($the_photo);
echo '<img src='.$the_photo.$bytes_photo.'>';
?>


so now, the image don't reload systematically, only when a change have been made (image replaced without changing filename)

this fix work fine for me

if you have a better solution.. please share it

thanks

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Real effective technique to refresh image after upload

Postby johnj » Thu Mar 17, 2011 3:55 am

php is for server side and the refresh has to happen on the client side. so, you can do this, <META HTTP-EQUIV="refresh" CONTENT="5"> - this will refresh the page every 5 seconds

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Real effective technique to refresh image after upload

Postby egami » Thu Mar 17, 2011 5:37 am

<?php
// line 3 through 5 //

ini_set('session.cache_limiter','private');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');

User avatar
gandahar
New php-forum User
New php-forum User
Posts: 112
Joined: Tue Mar 15, 2011 4:05 am

Re: Real effective technique to refresh image after upload

Postby gandahar » Mon Mar 21, 2011 4:00 am

johnj wrote:php is for server side and the refresh has to happen on the client side. so, you can do this, <META HTTP-EQUIV="refresh" CONTENT="5"> - this will refresh the page every 5 seconds


Thanks for this solution, but this is not effective in every case. Thanks for sharing

egami wrote:<?php
// line 3 through 5 //

ini_set('session.cache_limiter','private');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');


I've been trying this solution too, but sometime, the image don't refresh and still load from the browser cache. Thanks for sharing

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Real effective technique to refresh image after upload

Postby johnj » Mon Mar 21, 2011 4:31 am

If the file size doesn't change with the upload of a new image,you will still see the old image.

I don't understand why egami's soln did not work because the cache is forced to expire in the past.

User avatar
gandahar
New php-forum User
New php-forum User
Posts: 112
Joined: Tue Mar 15, 2011 4:05 am

Re: Real effective technique to refresh image after upload

Postby gandahar » Mon Mar 21, 2011 9:04 am

johnj wrote:If the file size doesn't change with the upload of a new image,you will still see the old image.

I don't understand why egami's soln did not work because the cache is forced to expire in the past.


yeah it's true, but when showing filezise in bytes, there is not much probability to have same filesize on two differents fotos....

i've been trying the egami's solution with firefox, ie and others, they sometimes still loading image from cache

User avatar
egami
php-forum GURU
php-forum GURU
Posts: 2196
Joined: Wed Oct 06, 2010 11:19 am
Location: Happy Valley, UT

Re: Real effective technique to refresh image after upload

Postby egami » Wed Mar 23, 2011 4:56 am

Try just using the header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); and comment out the others.


Return to “PHP coding => Images”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest