Home > php教程 > php手册 > PHP读取文件内容后清空文件示例代码

PHP读取文件内容后清空文件示例代码

WBOY
Release: 2016-06-13 09:40:33
Original
882 people have browsed it

复制代码 代码如下:


$fh = fopen($path, "r+");

if( flock($fh, LOCK_EX) ){//加写锁
$old_content=json_decode(fread($fh,filesize($path)),true);

$old_content=$old_content.$new_content;
ftruncate($fh,0); // 将文件截断到给定的长度
rewind($fh); // 倒回文件指针的位置
fwrite($fh,json_encode($old_content));
// @chmod($path,0644);
flock($fh, LOCK_UN); //解锁

}
fclose($fh);

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template