PHP force refresh image

后端 未结 5 2068
野性不改
野性不改 2020-12-06 11:00

I\'m creating an input form that allows users to upload an image. Once it has been submitted the user is returned to the form with a conformation bit at the top.

An

相关标签:
5条回答
  • 2020-12-06 11:27

    In PHP you can send a random number or the current timestamp:

    <img src="image.jpg?<?=Date('U')?>">
    

    or

    <img src="image.jpg?<?=rand(1,32000)?>">
    
    0 讨论(0)
  • 2020-12-06 11:31

    The trick here is to tell the browser to not cache the content. You can do this by putting these statements before ANYTHING else (no whitespace or anything!)

    <?php
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
    header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Pragma: no-cache");
    ?>
    

    Depending on how you're loading the image, you can also do a javascript refresh of the image using jQuery.

    0 讨论(0)
  • 2020-12-06 11:38

    Add the modified date to the end of the image as a query.

    <img src="/images/photo.png?=1239293"/>
    

    In PHP

    <img src="/images/photo.png?=<?php echo filemtime($filename)?>"/>
    
    0 讨论(0)
  • 2020-12-06 11:44

    You could replace the current image by a new one using JavaScript:

        <div id="imageHolder">this is where the image will show</div>
        <script type="text/javascript">
        function myfunction(){
        document.getElementById("imageHolder").innerHTML="<div id=\"imageHolder\"><img src='imagefolder/imagename.jpg' alt=''/></div>";
        }</script>
    

    You need to trigger the function myfunction after completing the upload of course ...

    0 讨论(0)
  • 2020-12-06 11:49

    This worked great for me:

    image.jpg?=<?php echo rand() . "\n";?>
    
    0 讨论(0)
提交回复
热议问题