<?php class cache { var $cache_dir = './cache/'; var $cache_time = 100000;// Thá»i gian để tạo cache má»›i var $caching = false; var $file = ''; function cache() { $this->file = $this->cache_dir . urlencode( $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] ); if ( file_exists ( $this->file ) && ( fileatime ( $this->file ) + $this->cache_time ) > time() ) { $handle = fopen( $this->file , "r"); do { $data = fread($handle, 8192); if (strlen($data) == 0) { break; } echo $data; } while (true); fclose($handle); exit(); } else { $this->caching = true; ob_start(); } } function close() { if ( $this->caching ) { $data = ob_get_clean(); echo $data; $fp = fopen( $this->file , 'w' ); fwrite ( $fp , $data ); fclose ( $fp ); } } } ?> Link : http://www.congnghemoigialai.vn/thiet-ke-website-gia-re-gia-lai/thu-thuat-seo---danh-cho-webmaster/thu-vien-cachephp---cache-website.html
Hy, I think that is better to use file_get_contents() to read the file, and file_put_contents() to write it, than fopen().