【问题标题】:How To fopen and write to a specified section on a file如何打开并写入文件的指定部分
【发布时间】:2012-08-02 01:17:24
【问题描述】:

我似乎无法弄清楚如何让这个脚本将数据写入文件的部分。以下是适用的代码摘录:

$file = $url.'.php';   
if (!$file_handle = fopen($file,"a+")) 
{ 
echo $lang['cannot_open_file']; 
} 
if (!fwrite($file_handle,stripslashes(html_entity_decode($data))))
{ 
echo $lang['file_is_not_writable']; 
}
fclose($file_handle);  
echo ($lang['success']);
echo ('<meta http-equiv="Refresh" content="4;url='.$url.'.php" />');
}
}

?>

<form action="<?php echo $url;?>.php" method="post">
<?php echo $lang['name']?> <span style="font-weight:100;">(<?php echo $lang['required']?>)</span>:<br />
<input class="textfield" type="text" name="name" value="" /><br />
<?php echo $lang['website']?> <span style="font-weight:100;">(<?php echo $lang['without_http'];?>)</span>:<br />
<input class="textfield" type="text" name="site" value="" /><br />
<?php echo $lang['message']?>:<br />
<textarea class="textarea" rows="2" cols="25" name="message"></textarea><br />
<img src="captcha-image.php" alt="Image verification" />  <br />
  <?php echo $lang['value_from_the_image'];?>:<br />
<input class="textfield" type="text" name="img_ver" value="" /><br />
        <input type="reset" name="reset" value="<?php echo $lang['reset'];?>" />
        <input type="submit" name="send" value="<?php echo $lang['send'];?>" />
      </form>
<hr>
<h3>Comments:</h3>
<!--comments -->

<?php include('../templates/footer.php'); ?>

有人介意帮我解决这个问题吗?将不胜感激。

【问题讨论】:

  • fseek()。如果我们不知道文件的外观或您想写入它的位置,则很难提供帮助。你能提供更多细节吗?
  • 你打赌,我会编辑帖子并添加整个文件。谢谢。

标签: php fopen explode fwrite fclose


【解决方案1】:
  1. 先使用文件获取内容来检索内容。
  2. 然后使用 preg_replace 查找要替换的位置或内容。
  3. 然后用新内容重写文件。

【讨论】:

    猜你喜欢
    • 2017-08-26
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多