缓存时PHP读写文件的方法

早期的php框架都是使用之前的方式,下面的为改进的,使用就方便了。

原始的php读入文件方式:

// $handle = fopen($files[0], 'r');
// flock($handle, LOCK_SH);//共享
// $data = fread($handle, filesize($files[0]));
// flock($handle, LOCK_UN);
// fclose($handle);

简单的读改为:

$data = file_get_contents($files[0]);

原始的写入文件缓存方式:

// $handle = fopen($file, 'w');
// flock($handle, LOCK_EX);//独占
// fwrite($handle, serialize($value));
// fflush($handle);
// flock($handle, LOCK_UN);
// fclose($handle);

简单的写入改为:

file_put_contents($file, serialize($value),LOCK_EX);//与依次调用 fopen(),fwrite() 以及 fclose() 功能一样。
原文地址:https://www.cnblogs.com/wyzs/p/5162300.html