Mik*_*e B 78
file_put_contents('file.txt', 'bar');
echo file_get_contents('file.txt'); // bar
file_put_contents('file.txt', 'foo');
echo file_get_contents('file.txt'); // foo
Run Code Online (Sandbox Code Playgroud)
或者,如果您遇到困难,fopen()可以使用w或w+模式:
'w' 仅供写作; 将文件指针放在文件的开头,并将文件截断为零长度.如果该文件不存在,请尝试创建它.
'w +' 开放阅读和写作; 将文件指针放在文件的开头,并将文件截断为零长度.如果该文件不存在,请尝试创建它.
小智 7
$fname = "database.php";
$fhandle = fopen($fname,"r");
$content = fread($fhandle,filesize($fname));
$content = str_replace("192.168.1.198", "localhost", $content);
$fhandle = fopen($fname,"w");
fwrite($fhandle,$content);
fclose($fhandle);
Run Code Online (Sandbox Code Playgroud)
我的首选方法是使用fopen、fwrite 和 fclose [它会花费更少的 CPU]
$f=fopen('myfile.txt','w');
fwrite($f,'new content');
fclose($f);
Run Code Online (Sandbox Code Playgroud)
警告那些使用file_put_contents 的人
它会对性能产生很大影响,例如[在同一类/情况下] file_get_contents也是:如果您有一个大文件,它会一次性读取整个内容,并且该操作可能需要很长时间的等待时间
| 归档时间: |
|
| 查看次数: |
97499 次 |
| 最近记录: |