在阅读了许多有关编辑/更新文件功能的类似问题后,我发现这些方法都无效,因此希望得到一些帮助。
我正在尝试从php中编辑一个.txt
文档。 我已经尝试了以下几种方法:
This was the last code which I've read here and didn't work.
$data_to_write = "$_POST[subject]"; $file_path = "text/" + $row['name']; $file_handle = fopen($file_path, 'w'); fwrite($file_handle, $data_to_write); fclose($file_handle);
And this is my previous try:
$new_contents = "$_POST[subject]\n"; $path = "text/$row[name]"; file_put_contents($path, $new_contents);
我希望有人可以向我解释如何正确地完成这个操作。谢谢。
以下是我的所有代码:
<?php
if(isset($_GET['id']))
{
$edit = mysql_query("SELECT * FROM text_area WHERE text_id=$_GET[id]");
$row = mysql_fetch_assoc($edit);
$contents = file_get_contents($row['content']);
?>
<form action="" name="form" method="post">
<input type="hidden" name="id" value="<?php echo $row['text_id']; ?>" /><br />
<label for="">Заглавие:</label><br />
<input type="text" name="title" style="width:500px;" value="<?php echo $row['subject'] ?>" /><br />
<select name="opt">
<option value="0"></option>
<?php
$result = mysql_query("SELECT * FROM image_area");
while ($row = mysql_fetch_array($result))
{
echo "<option value=" . $row['path'] . ">" . $row['name'] . "</option>
";
}
?>
</select><input type="button" name="sumbitP" value="Choose" onclick="addtext();" /><a href="../image_list.php" target="_blank">Image list</a><br />
<textarea rows="10" cols="50" name="text" id="markItUp"><?php echo $contents ?></textarea><br />
<input type="submit" name="sumbitT" value="Update" />
<input type="reset" value="Reset" />
</form>
<?php
}
?>
<?php
if(isset($_POST['id']))
{
if(mysql_query("UPDATE text_area SET title='$_POST[subject]' WHERE text_id ='$_POST[id]'"))
{
$data_to_write = "" . $_POST['text'];
$file_path = "text/$row[name]";
$file_handle = fopen($file_path, 'w');
fwrite($file_handle, $data_to_write);
fclose($file_handle);
echo '<br><br><p align="center">Everything is ok</p>';
} else {
echo '<br><br><p align="center">Everything is not ok</p>' ;
}
?>
我想补充一些可能有用的东西:
我遇到了这个错误,但是在谷歌上找不到答案。
警告:fopen(text /)[function.fopen]:无法打开流:是目录
ini_set('display_errors', 1);
和error_reporting(E_ALL);
,这样错误信息将会显示出来,您可以看到为什么它不起作用。 - Matt