: This article mainly introduces simple file classes. if you are interested in the PHP Tutorial, you can refer to it.
1
Dir = $ dir; 12 13} 14/** 15 * @ desc sets the file cache 16 * @ param string $ key file name 17 * @ param unkonw $ data cache data 18 * @ param int $ expire expiration time 19 */20 public function set ($ key, $ data, $ expire = 0) {21 $ this-> filename = dirname (_ FILE __). self: C_FILE. $ this-> dir. $ key. self: EXT; 22 if (file_exists ($ this-> filename) {23 $ res = $ this-> get ($ key); 24 if (md5 ($ res) = md5 (json_encode ($ data) {25 return true; 26} 27} 28 if (! Is_dir (dirname ($ this-> filename) {29 mkdir (dirname ($ this-> filename), 0777 ); 30} 31 32 $ source = fopen ($ this-> filename, 'W + '); 33 fwrite ($ source, json_encode ($ data )); 34 fclose ($ source); 35} 36 37/** 38 * @ desc get file 39 * @ param string $ key file name 40 */41 public function get ($ key) {42 // $ filename = dirname (_ FILE __). self: C_FILE. $ this-> dir. $ key. self: EXT; 43 if (! File_exists ($ this-> filename) {44 return 'the cache file does not exist'; 45} else {46 $ res = file_get_contents ($ this-> filename ); 47} 48 return $ res; 49} 50/** 51 * @ desc delete file 52 * @ param string $ key file name 53 */54 public function del ($ key) {55 unlink ($ this-> filename); 56} 57 58} 59 60 $ data = array ('name' => 'hangzhou', 'age' => 20, 'Sex' => 'Man ', 'favority' => array ('apple', 'bana'); 61 $ cache = new Cache (); 62 $ cache-> set ('cache', $ data); 63 // $ cache-> get ('cache '); 64 // $ cache-> del ('cache ');
The above introduces simple file classes, including the content, and hope to be helpful to friends who are interested in PHP tutorials.