We talked before how we can cache with php. In this post I will show you how we can clear cache. So, for caching a website in php we said we need a directory were we will store all the pages that are showing to the user and save the output to a folder on our server. So in order to clear the cache you should delete all the files from directory that were created.
To do that we only need to loop through the cache folder(go here for learning how to get all the files from a folder) and delete all the files that are inside that folder. Remember to change permissions 777 to cache directory. Here is the code to do that
<? $cachedir = "../cache/"; if ($cachehandle = opendir($cachedir)) { while (false !== ($file = readdir($cachehandle))) { if ($file != "." && $file != "..") { $file2del = $cachedir."/".$file; unlink($file2del); } } closedir($cachehandle); } echo 'Cached cleared.'; ?>


















1 Comment to 'PHP clear cache directory'
May 23, 2009
Another way to quickly clean out a folder of files:
Leave a comment
You must be logged in to post a comment.