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:
-
/**
-
* :: Delete the specified directory for deleting the cache
-
* @param [type] $dir_path Folder Path
-
* @return [type] [description]
-
*/
-
function deleteDir($dir_path)
-
{
-
$dh = opendir($dir_path);
-
while (($file = readdir($dh)) !== false){
-
if ($file != "." && $file != "..") {
-
$fullpath = $dir_path.'/'.$file;
-
if(!is_dir($fullpath)){
-
unlink($fullpath);
-
}else{
-
deleteDir($fullpath);
-
rmdir($fullpath);
-
}
-
}
-
}
-
closedir($dh);
-
$result = true;
-
$ndh = opendir($dir_path);
-
while (($file = readdir($ndh)) !== false){
-
if($file != "." && $file != ".."){
-
$result = false;
-
}
-
}
-
closedir($ndh);
-
return $result;
-
}
Call function:
-
// Clear the cache
-
public function deleteCache()
-
{
-
$retval = deleteDir('../runtime/cache');
-
if($retval){
-
$retval = deleteDir('../runtime/temp');
-
}
-
return $retval;
-
}