Hello all. I want to know how to clear cache in PHP. I am updating a image from admin panel. But after uploading old images displays. When i refresh the page then the new image dispaly. Can any one solve this problem. Thanks in advance
Its got nothing to do with PHP. Its the browser that store image cache and rightly replaces it with the new image when you refresh . So you don't need to worry at all
The cache is on the client side not the server side, so this can't be done in PHP. A solution might be to put a query string and a ever changing value after the source of the image like: echo '<img src="'.$imageSource.'?'.gmdate('U').'" />'; Code (markup):
Yes that could be done , but then , it would also mean you ask an extra variable to get processed for a not-so-big-deal Therefore , use it only if its absolutely necessary . I dont think it is though Good luck with your website.
Unless the server is very very slow, it's going to take well under a millisecond to process that, and actually having the new image show is the only deal asked for.
I was assuming that there are tons of other images as well . So that would mean tons of other variables . oh well . . .different people . Different opinions . . .
If there are multiple images, you could use: $now = gmdate('U'); echo'<img src="'.$imageSource1.'?'.$now.'" /> <img src="'.$imageSource2.'?'.$now.'" /> <img src="'.$imageSource3.'?'.$now.'" /> <img src="'.$imageSource4.'?'.$now.'" /> <img src="'.$imageSource5.'?'.$now.'" /> <img src="'.$imageSource6.'?'.$now.'" /> '; Code (markup): Or if the image sources are in an array $imageSource = array('1.jpg', '2.jpg', '3.jpg', '4.jpg', '5.jpg', '6.jpg'); $now = gmdate('U'); foreach ($imageSource as $var){ echo'<img src="'.$var.'?'.$now.'" />'; } Code (markup): meaning you'd only have to generate a fresh string to put on the query string once. If generating it by using gmdate() (or a similar date/time function) proves to resource intensive, rand() could be used instead to generate a random number which if your browser hasn't cached before will cause it to load the new image .