如何用PHP的新内容覆盖文件内容?

时间:2022-02-18 00:40:11

I tried to use fopen, but I only managed to append content to end of file. Is it possible to overwrite all contents with new content in PHP?

我尝试使用fopen,但是我只把内容附加到文件的末尾。是否可以用PHP覆盖所有内容?

3 个解决方案

#1


60  

Use file_put_contents()

使用用file_put_contents()

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

Alternatively, if you're stuck with fopen() you can use the w or w+ modes:

或者,如果你被fopen()困住了,你可以使用w或w+模式:

'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

“w”只供写作使用;将文件指针放置在文件的开头,并将文件截断为零长度。如果文件不存在,尝试创建它。

'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

“w+”开启阅读和写作;将文件指针放置在文件的开头,并将文件截断为零长度。如果文件不存在,尝试创建它。

#2


1  

$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);

#3


1  

Alternatively if you have full control of your server, why not "guarantee" the operation tweaking it around by removing the file before?

或者,如果您完全控制了服务器,为什么不“保证”通过删除之前的文件来调整操作?

$wait=exec('rm -r -f myfile.txt');
file_put_contents('myfile.txt','new content');

The $wait here is just to hold the remove command until it gets done, and make the file_put_contents safe because sometimes it only appends content, not replacing for a new one

这里的$wait只是保存remove命令,直到它完成,并使file_put_contents变得安全,因为有时它只附加内容,而不是替换新的内容

MY PREFERRED METHOD [the most effective of all] is using fopen,fwrite and fclose [it will cost less CPU and never fails]

我的首选方法[最有效的方法]是使用fopen、fwrite和fclose[它将花费更少的CPU并且不会失败]

$f=fopen('myfile.txt','w');
fwrite($f,'new content');
fclose($f);

#1


60  

Use file_put_contents()

使用用file_put_contents()

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

Alternatively, if you're stuck with fopen() you can use the w or w+ modes:

或者,如果你被fopen()困住了,你可以使用w或w+模式:

'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

“w”只供写作使用;将文件指针放置在文件的开头,并将文件截断为零长度。如果文件不存在,尝试创建它。

'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

“w+”开启阅读和写作;将文件指针放置在文件的开头,并将文件截断为零长度。如果文件不存在,尝试创建它。

#2


1  

$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);

#3


1  

Alternatively if you have full control of your server, why not "guarantee" the operation tweaking it around by removing the file before?

或者,如果您完全控制了服务器,为什么不“保证”通过删除之前的文件来调整操作?

$wait=exec('rm -r -f myfile.txt');
file_put_contents('myfile.txt','new content');

The $wait here is just to hold the remove command until it gets done, and make the file_put_contents safe because sometimes it only appends content, not replacing for a new one

这里的$wait只是保存remove命令,直到它完成,并使file_put_contents变得安全,因为有时它只附加内容,而不是替换新的内容

MY PREFERRED METHOD [the most effective of all] is using fopen,fwrite and fclose [it will cost less CPU and never fails]

我的首选方法[最有效的方法]是使用fopen、fwrite和fclose[它将花费更少的CPU并且不会失败]

$f=fopen('myfile.txt','w');
fwrite($f,'new content');
fclose($f);