0

This is what I am doing check if file exists and echo in the page, but page shows the old image even when it's already replaced by another image it just shows the old one, but the new image is there and the old one was replaced by the new image with the same name.

<?php 

$dir = 'up/images/'.$_SESSION ['username'].'.png';

if (file_exists($dir)) {
    //echo "ok";
  echo '<img  src="up/images/'.$_SESSION['username'].'.png" class="img-responsive img-thumbnail" width="200px;" height="200px;">'; 

} else {
   //not found 

  echo '<img  src="img/user-icon-yellow.png" class="img-responsive img-thumbnail" width="200px;" height="200px;">'; 
}


?>

It looks like a cache problem or something.

Otávio Barreto
  • 1,348
  • 3
  • 10
  • 27
  • Set the cache headers with PHP. http://stackoverflow.com/questions/13640109/how-to-prevent-browser-cache-for-php-site – Variable Apr 25 '17 at 14:06
  • Can you rephrase your question, it's not clear at all what you're asking. – Jan Hančič Apr 25 '17 at 14:06
  • for a quick test you can trick the browser/ISP cache by adding a random string to the "query string" of the image... imagename.php?SomethingRandomHere – Jen R Apr 25 '17 at 14:07

3 Answers3

1

A good way to cache bust would be to add version strings to your image paths. For example:

$image = basename($_SESSION['username']) . ".png";
$image = realpath("/path/to/$image");
echo '<img src="up/images/'.$_SESSION['username'].'.png?v='.filemtime($image).'" [...]>';

This is assuming your HTML pages aren't being cached, if they are, then you'll need to send additional headers to bust the cache. This varies, depending on the environment, but for something like Apache you could do the following:

 Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
 Header set Pragma "no-cache"
 Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"

Or, alternatively, you could use PHP's header() function to set the headers:

 header("Cache-Control: max-age=0, no-cache, no-store, must-revalidate");
 header("Pragma: no-cache");
 header("Expires: Wed, 11 Jan 1984 05:00:00 GMT");
Rob W
  • 8,825
  • 1
  • 26
  • 48
  • could you give a implementation of this header in php? should add as string or not? please give a implementation in header function – Otávio Barreto Apr 25 '17 at 14:23
  • It is partially working, but when the user uploads an image, it is done via ajax and the file sent in an external form, so the page is not automatically reloaded after the upload, so it is necessary to press f5 to show the new image, How To do this automatically? I did with header location but it did not work. – Otávio Barreto Apr 25 '17 at 14:34
  • 1
    Use JS to reload the image after the AJAX upload is complete while appending a random string to the tail of the URL as a query string. For example (in jquery for ease): `var $image = $("selector to img"); $image.prop("src", $image.prop("src") + "?" + Math.rand())` -- update the selector, of course. – Rob W Apr 25 '17 at 14:36
  • I added this in the save button `onClick="window.location.reload()"` but did't' work – Otávio Barreto Apr 25 '17 at 14:37
  • I discovery why it did't work because `onClick="window.location.reload()"` runs on the time image still is uploading so it need to run the refresh after 1 second for example – Otávio Barreto Apr 25 '17 at 14:52
  • Solved with the following `` – Otávio Barreto Apr 25 '17 at 15:18
  • You shouldn't assume 1 second -- but reload the page after the file has uploaded (in a callback). – Rob W Apr 25 '17 at 19:31
  • Like on submit event? – Otávio Barreto Apr 25 '17 at 19:36
  • Nope - however you are handling the AJAX file upload, there should be a callback you can create that fires a function (page reload, or reload the image) so that you can be certain the upload finished. – Rob W Apr 25 '17 at 19:45
  • I was not running ajax in this form , I confused with other forms in the same page the form: `
    `
    – Otávio Barreto Apr 25 '17 at 19:51
0

Make sure you really replaced the image. And also the image won't change until you refresh the page. PHP is not a dynamic language (the result (the new image) won't show up in the moment you change it).
When you replace the image do this to refresh the page:

header("Refresh:0"); //Refreshes the page after 0 second (immediately)

Also you can try using:

header('Location: '. $_SERVER['PHP_SELF']); //It will redirect the page to the same page and probably clear the cache
XTard
  • 56
  • 8
  • Yes the image is really replaced `header("Refresh:0");` same problem when refresh – Otávio Barreto Apr 25 '17 at 14:18
  • 1
    Yeah then it is really a cache problem, i guess it is because the filenames are the same and it just loads the old image, by the way instead of this use `header('Location: '. $_SERVER['PHP_SELF']);`, i think it will clear the cache and 'refresh' the page. – XTard Apr 25 '17 at 14:37
-1

It's cache. Try to hit CTRL + F5 on Google Chrome or just go in Settings and remove cached files.

You can always install Google Chrome extension which can delete cached files for particular website on each refresh.