一定并发量下,在硬盘上写入文件
发布时间:2020-05-30 23:49:17 所属栏目:PHP 来源:互联网
导读:一定并发量下,在硬盘上写入文件
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 <?php /** * Created by PhpStorm. * User: [emailprotected] * Date: 15/10/22 * Time: 下午5:12 */ function write_log($log_content) { $log_file = '/logs/error.log'; if(is_file($log_file)) { // 检测log文件大小,将每个log文件控制在2m以内 $log_filesize = filesize($log_file); $max_size = 2 * 1024 * 1024; // 可以接受的最大的文件大小 if($log_filesize >= $max_size) { $new_log_file = '/logs/error_' . date('YmdHis') . '.log'; rename($log_file,$new_log_file); } } $fp=fopen($log_file,'a+'); if($fp){ $startTime=microtime(); do{ // 这个循环可以保证进程在尝试1m后,如果未能锁定文件,则放弃写入日志的操作 $canWrite=flock($fp,LOCK_EX); if(!$canWrite){ usleep(round(rand(0,100)*1000)); } }while((!$canWrite)&&((microtime()-$startTime)<1000)); if($canWrite){ $content = date('Y-m-d H:i:s') . ' ' . $log_content . "rn"; fwrite($fp,$content); } fclose($fp); } } 以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |