开发者

Find and delete all cache files on server

I'm caching each page in its own directory rather than in a common directory:

www/contact/index.php
www/contact/index.php.cache

It's simpler to implement but now I must clear e开发者_运维问答ach file manually.

Ideally I'd like to run www/clear-cache.php and have it find and delete all files that end in .php.cache anywhere in www/

I ask here cause I've already update the site and now I'm kinda in a hurry to clear the cache.


in php this can be done by using the following code

function delete_cache($path, $pattern,) {
  $path = rtrim(str_replace("\\", "/", $path), '/') . '/';
  $matches = Array();
  $entries = Array();
  $dir = dir($path);
  while (false !== ($entry = $dir->read())) {
    $entries[] = $entry;
  }
  $dir->close();
  foreach ($entries as $entry) {
    $fullname = $path . $entry;
    if ($entry != '.' && $entry != '..' && is_dir($fullname)) {
      delete_cache($fullname, $pattern);
    } else if (is_file($fullname) && preg_match($pattern, $entry)) {
      unlink($fullname); // delete the file
      echo $fullname," deleted.<br />"; #comment out if you do not want to echo the file deleted.
    }
  }
}

This goes through each file and directory looking for any files that contain cache in the file name and delete it and is called by:

delete_cache(getenv("DOCUMENT_ROOT"), "/cache/i"); //Starts in the www/htdocs folder

I will state to backup the site first to make sure that it does not delete a file that is needed that has cache in the filename. Or if you want an automatic cache system for your site, you can check out my answer on Simple PHP caching with more than 3 parts


If you run linux try something like this:

find . -name "*.php.cache" | xargs rm -f
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜