gpt4 book ai didi

php - 更改文件的单个字符

转载 作者:行者123 更新时间:2023-12-02 05:44:18 24 4
gpt4 key购买 nike

我需要更改文件中的单个字符。
我宁愿不使用像这样的辅助文件(伪代码):
读取(文件)

更改(文件)

写入(theFile2)

删除(文件)

重命名(theFile2,theFile)

因为这样,某些进程可以在实际删除该文件时要求该文件。
相反,我想对自己的文件进行操作,因为通过这种方式,我依靠服务器的操作系统来处理时间和处理对文件的访问。

最佳答案

您需要使用flock 来锁定文件。示例基于 PHP docs 中的代码:

<?php

$fp = fopen("file.txt", "r+");

if (flock($fp, LOCK_EX)) { // acquire an exclusive lock
//make your changes
fflush($fp); // flush output before releasing the lock
flock($fp, LOCK_UN); // release the lock
} else {
echo "Couldn't get the lock!";
}

fclose($fp);

?>

关于php - 更改文件的单个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10487505/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com