SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Fionnphort, Isle of Mull, Scotland
    Posts
    358
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Can PHP clear the browser cache and refresh ?

    I have a script which allows a visitor to the web site to upload an image. Since this replaces an earlier image, it gets given the same name (desirably). The upload, and the renaming happen OK. But when all is committed to the database and the visitor is shown the result, the old image is still visible. Clearing the browser cache and refreshing the screen (manually) shows the new image.

    Is there a way I can get PHP (or JS, I suppose) to do this automatically as part of the script ?
    Tim Dawson
    Isle of Mull, Scotland

  2. #2
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    one way to force a reload of the image every time the page is loaded is to append a unique query string to the filename of the <img>'s src. So even though the filename will be the same, the unique query string will be different thus forcing the browser to download the image again because the url for the image will be different on each page load.

    I normally use the current date/time for the unique query string.

  3. #3
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Fionnphort, Isle of Mull, Scotland
    Posts
    358
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks. That's a good suggestion. At first sight I wasn't sure if I could do that, because the file name is being drawn from a database (one reason for retaining the same image filename). But it should be possible to append a query string at some later point. I'll look into it.
    Tim Dawson
    Isle of Mull, Scotland

  4. #4
    Non-Member Kalon's Avatar
    Join Date
    Aug 2010
    Location
    At my computer
    Posts
    2,012
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you're welcome

    I would set the query string on the server so that you don't have to worry about javascript disabled browsers.

    something along this line:

    Code:
     
    <?php
    
    $today = date("F j, Y, g:i a");
     
    echo '<img src="pic1.jpg?myPic='.urlencode($today).'" alt="" />';
     
    ?>
    in your case you would need to build the string for the img src by appending the current date/time to the file name of the image that was retrieved from the database

  5. #5
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Fionnphort, Isle of Mull, Scotland
    Posts
    358
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the extra pointers. I've got it set up much as you suggest, and it seems to be working a treat (why would it not ?). A bit more testing, though, to make sure there's no unexpected consequence.

    To make matters more exciting, it's all within a loop so I've got to make sure the date/time variable gets refreshed every pass, or it will fail in it's purpose. But that's just detail. The main problem is resolved. Thanks again.
    Tim Dawson
    Isle of Mull, Scotland


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •