企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
`~~~ /** * 向文件头部加入内容 * @param string $fileName * @param $append */ private function writeFileAtFirstLine($fileName='',$append){ @set_time_limit(0); $file = fopen($fileName, 'r'); $content = fread($file, @filesize($fileName)); $contents = $append . PHP_EOL . $content; fclose($file); $file = fopen($fileName, 'w'); $res = fwrite($file, $contents); fclose($file); if ($res >0){ return true; }else{ return false; } } ~~~` `~~~ /** * 向文件尾部加入内容 * @param string $fileName * @param $append * @return bool */ private function writeFileAtLasttLine($fileName='',$append){ @set_time_limit(0); $res = file_put_contents($fileName, $append, FILE_APPEND); if ($res>0){ return true; }else{ return false; } } ~~~` `~~~ /** * 取文件最后$n行 * @param string $filename 文件路径 * @param int $n 最后几行 * @return mixed false表示有错误,成功则返回字符串 */ private function FileLastLines($filename,$n){ if(!$fp=fopen($filename,'r')){ echo "打开文件失败,请检查文件路径是否正确,路径和文件名不要包含中文"; return false; } $pos=-2; $eof=""; $str=""; while($n>0){ while($eof!="\n"){ if(!fseek($fp,$pos,SEEK_END)){ $eof=fgets($fp); $pos--; }else{ break; } } $str.=fgets($fp); $eof=""; $n--; } return $str; } ~~~` `~~~ /** * 获取指定行内容 * * @param $filePath文件路径 * @param $line 行数 * @param $length 指定行返回内容长度 */ private function getLineContent($filePath, $lineNum, $length = 5000){ $result = null; // 初始化返回 $i = 1; // 行数 $handle = @fopen($filePath, "r"); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle, $length); if($lineNum== $i) $result = $buffer; $i++; } fclose($handle); } return $result ; } ~~~` `~~~ /** * 删除文件的前几行 并返回删除后的数据 * @param $path * @param $line * @return string */ public function removeFileLines($path,$line){ $data = ""; $str = ""; chmod($path, 0777); $handle = fopen($path, 'rw'); for ($i=0; $i<$line;$i++) fgets($handle); ob_start(); while(!feof($handle)){ $str=fgets($handle, 1024); $data = $data.$str; } fclose($handle); file_put_contents($path, $data); ob_end_clean(); return $data; } ~~~` `~~~ /** * 生成新文件并写入加密内容 * @param $TxtFileName * @param $StrConents * @return bool */ private function WriteFile($TxtFileName,$StrConents){ @set_time_limit(0); $myfile = fopen($TxtFileName, "w") or die("Unable to open file!"); fwrite($myfile, $StrConents); fclose($myfile); return true; } ~~~`