web123456

thinkphp5 Delete Cache

First learn these php functions:

$dir = '. /runtime'; // Cache folder

is_dir($dir); // determine if it is a directory bool(true)

$dh = opendir($dir) ; // open directory handle resource resource(62) of type (stream) is a resource type file stream

closedir($dh); // close the directory handle note its argument, which is the resource type of opendir($dir)

readdir($dh); // return the filename of the next file in the directory, note its argument, which is the resource type of opendir($dir)

unlink(''); // function deletes the file

rmdir($dir); // function removes empty directory

Main code below:

  1. /**
  2. * :: Delete the specified directory for deleting the cache
  3. * @param  [type] $dir_path Folder Path
  4. * @return [type]          [description]
  5. */
  6. function deleteDir($dir_path)
  7. {
  8.     $dh = opendir($dir_path);
  9.     while (($file = readdir($dh)) !== false){
  10.         if ($file != "." && $file != "..") {
  11.             $fullpath = $dir_path.'/'.$file;
  12.             if(!is_dir($fullpath)){
  13.                 unlink($fullpath);
  14.             }else{
  15.                 deleteDir($fullpath);
  16.                 rmdir($fullpath);
  17.             }
  18.         }
  19.     }
  20.     closedir($dh);
  21.     $result = true;
  22.     $ndh = opendir($dir_path);
  23.     while (($file = readdir($ndh)) !== false){
  24.         if($file != "." && $file != ".."){
  25.             $result = false;
  26.         }
  27.     }
  28.     closedir($ndh);
  29.     return $result;
  30. }

Call function:

  1. // Clear the cache
  2. public function deleteCache()
  3. {
  4.   $retval = deleteDir('../runtime/cache');
  5.   if($retval){
  6.       $retval = deleteDir('../runtime/temp');
  7.   }
  8.   return $retval;
  9. }