最近在写运维开发的时候,经常遇到一些常见的文件操作。特别是在处理高并发需求时,需要同时操作REDISDOCUMENTDB。如果业务人员花太多时间在文件处理上,会降低开发效率。因此,笔者贴出了自己在开发中经常使用的几个功能,并提供了Copy和paste给大家。如果代码有什么问题,也希望大家能够提出来。1.将字串写入文件中w:读写打开;将文件指针放在文件的开头将文件截断为零长度。a:读写打开;将文件指针放在文件的末尾。函数inputStrToFile($filename,$writetext,$openmod='w'){$fp=fopen($filename,$openmod);如果($fp){群($fp,2);fwrite($fp,$writetext);fclose($fp);返回真;}else{返回错误;}}2.读取文件字符串functiongetFileToStr($fileName){$buffer='';if(!file_exists($fileName)){thrownewException('文件不存在',0);}$handle=fopen("./star_star_academy_set_10.txt","r");while(!feof($handle)){$buffer=$buffer.fgets($句柄);}fclose($handle);return$buffer;}3.把JSON文件转为数组functiongetJsonFileToArr($fileName){if(!file_exists($fileName)){thrownewException('文件不存在',0);}//只读打开;将文件指针放在b文件的开头$file=fopen($fileName,'r');$filestr=fread($file,filesize($fileName));fclose($文件);returnjson_decode($filestr,'true');}4.从数据库中读取数据,存入JSON文件functiongetDbToJsonFile($tableName,$fileName){//数据库操作$data=$this->db->选择($表名,'*');$filestr=json_encode($data);//打开读写;将文件指针放在文件的开头//将文件截断为零长度。$file=fopen($this->filePath.$fileName,'w');fwrite($file,$filestr);fclose($file);}5.将数组写入文件functioninputArrToFile($filename,$array,$valueName){$cachefile=$filename;$cachetext="
