您的位置:首页 > 编程语言 > PHP开发

php 快速读取txt或文本

2015-10-11 20:47 513 查看
一。读取第一行后删除

$fn = "list2.txt";

$f= fopen($fn, "r");

$line = fgets($f);

ob_start();

fpassthru($f);

fclose($f);

file_put_contents($fn, ob_get_clean() );


其中
fgets($f) 取得第一行
二、文件写入最后行file_put_contents($this->logFile, $text, FILE_APPEND | LOCK_EX);
 public function export()    {        $text = implode("\n", array_map([$this, 'formatMessage'], $this->messages)) . "\n";        if (($fp = @fopen($this->logFile, 'a')) === false) {            throw new InvalidConfigException("Unable to append to log file: {$this->logFile}");        }        @flock($fp, LOCK_EX);        if ($this->enableRotation) {            // clear stat cache to ensure getting the real current file size and not a cached one            // this may result in rotating twice when cached file size is used on subsequent calls            clearstatcache();        }        if ($this->enableRotation && @filesize($this->logFile) > $this->maxFileSize * 1024) {            $this->rotateFiles();            @flock($fp, LOCK_UN);            @fclose($fp);            @file_put_contents($this->logFile, $text, FILE_APPEND | LOCK_EX);        } else {            @fwrite($fp, $text);            @flock($fp, LOCK_UN);            @fclose($fp);        }        if ($this->fileMode !== null) {            @chmod($this->logFile, $this->fileMode);        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: