PHP關於文章類的快取檔案(靜態檔案)生成
阿新 • • 發佈:2018-12-20
前幾天我們組長跟我說了一個需求,是關於生成分類下對應的快取檔案,具體來說就是根據文章所屬的分類ID(或者對應的file_name欄位)去新建一個目錄,然後將該分類下的文章生成一個靜態檔案。說這樣利於SEO優化。用到的檔案、目錄操作函式很多。現在總結一下
用到的系統函式:
//獲取當前目錄的上一級目錄的絕對路徑 realpath(dirname(__FILE__).'/../'); --------------------------------------------- //返回當前目錄的絕對路徑 dirname(__FILE__) --------------------------------------------- //掃描一個目錄(路徑)下的所有目錄 scandir($dir) --------------------------------------------- //判斷該檔案是否存在,return true;false file_exists($file_name) ---------------------------------------------
在刪除快取中,用到的自定義函式:
//刪除目錄下的檔案(不刪除目錄) function deldir($dir){ $dh = opendir($dir); while ($file = readdir($dh)) { if($file != "." && $file!="..") { $fullpath = $dir."/".$file; if(!is_dir($fullpath)) { unlink($fullpath); } else { deldir($fullpath); } } } closedir($dh); return true; }