Follow me on Twitter to receive updates, free scripts and ongoing announcements!

PHP clear cache directory

Written by Codes Tips on April 18, 2009 – 12:09 pm -

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.';
?>

Tags:
Posted in PHP/MYSQL |

One Comment to “PHP clear cache directory”

  1. timNo Gravatar Says:

    Another way to quickly clean out a folder of files:

    $files = glob('script/*');
    $files = array_filter($files, 'is_file');
    array_map('unlink', $files);

Leave a Comment

You must be logged in to post a comment.


Plugintaylor.com - Plugintaylor and Search Engine Optimization