Home  >  Article  >  Backend Development  >  PHP clears the file after reading the file content sample code_PHP tutorial

PHP clears the file after reading the file content sample code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:35:39991browse

Copy code The code is as follows:

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

if( flock($fh, LOCK_EX) ){//Add write lock
$old_content=json_decode(fread($fh,filesize($path)),true);

$old_content=$old_content.$ new_content;
ftruncate($fh,0); // Truncate the file to the given length
rewind($fh); // Rewind the position of the file pointer
fwrite($fh,json_encode( $old_content));
// @chmod($path,0644);
flock($fh, LOCK_UN); //Unlock

}
fclose($fh);

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/743151.htmlTechArticleCopy the code The code is as follows: $fh = fopen($path, "r+"); if( flock($fh , LOCK_EX) ){//Add write lock $old_content=json_decode(fread($fh,filesize($path)),true); $old_content=$old_content.$new...
Statement:
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