我第一次尝试使用fopen,并想知道在用数据添加或替换该内容之前,是否可以在文件中搜索特定的部分?
理想情况下,我想:
fopen获取文件fopen的注释,该注释包含新的数据。这可能吗?(对于将数据追加到文件末尾或向特定行号添加新数据的记录,由于文件在不断更改,因此对我正在处理的内容不起作用)。
谢谢!
发布于 2011-11-04 20:56:39
如果您在其中读取了整个文件,然后使用str_replace进行更改,您应该能够得到所需的内容。
发布于 2011-11-04 20:59:12
<?php
// make sure radio is set
if( isset($_POST['enableSocialIcons']) )
{
// Open file for read and string modification
$file = "/test";
$fh = fopen($file, 'r+');
$contents = fread($fh, filesize($file));
$new_contents = str_replace("hello world", "hello", $contents);
fclose($fh);
// Open file to write
$fh = fopen($file, 'r+');
fwrite($fh, $new_contents);
fclose($fh);
}
?> 来自:http://www.php.net/manual/en/function.fopen.php#81325
编辑:查看您的表单到底发送了什么内容,在您要发布的文件顶部执行以下操作:
<?php
echo '<pre>';
print_r($_POST);
echo '</pre>';
exit;
?>https://stackoverflow.com/questions/8015588
复制相似问题