如何使用 php 编辑/更新 txt 文件


How to edit/update a txt file with php

在我读到了很多关于文件编辑/更新功能的类似问题并且没有一个工作之后,我想寻求一些帮助。

我正在尝试从 php 编辑.txt文档。我尝试过这些事情:

  1. 这是我在这里阅读的最后一个代码,但不起作用。

    $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);
    
  2. 这是我之前的尝试:

    $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>' ;
    }
?>

只是为了添加一些可能有用的东西:我
收到此错误,我无法通过Google找到答案。
Warning: fopen(text/) [function.fopen]: failed to open stream: Is a directory in

你只需要改变:

$file_path = "text/" + $row['name'];

对此:

$file_path = "text/" . $row['name'];

PHP 中的连接运算符是 .(不是 +

并确保目录text存在,否则最好检查然后写入:

$data_to_write = $_POST['subject'];
$file_path = "text/" . $row['name'];
if ( !file_exists("text") )
    mkdir("text");
$file_handle = fopen($file_path, 'w'); 
fwrite($file_handle, $data_to_write);
fclose($file_handle);

您还可以使用 fopen() 在追加模式下打开文件,并将您拥有的任何内容放在末尾

$path = dirname(__FILE__).'/newfile.txt';
$fp = fopen($path, 'a');
if(!$fp){
echo 'file is not opend';
}
fwrite($fp, 'this is simple text written');
fclose($fp);

您需要使用 file_get_contents 来获取文件的文本。

$file_path= "text/" . $row['name'];
// Open the file to get existing content
$current = file_get_contents($file_path);
// Append a new person to the file

$data_to_write.= $_POST[subject]."'n";
// Write the contents back to the file
file_put_contents($file_path, $data_to_write);

请参阅文档

我更喜欢使用file_put_contents和设置标志来添加文本而不是覆盖整个文件。使用标志,如果有多个脚本同时访问同一文件,也可以锁定文件。

方法如下:

$file_name = 'text.txt';
$line= "This is a new line'n";
 
// use flag FILE_APPEND to append the content to the end of the file
// use flag LOCK_EX to prevent other scripts to write to the file while we are writing
file_put_contents($file_name , $line, FILE_APPEND | LOCK_EX);

以下是完整的文档:https://www.php.net/manual/en/function.file-put-contents.php

我认为问题可能是第一行:

$data_to_write = "$_POST[subject]";

将其替换为以下内容:

$data_to_write = "" . $_POST['subject'];

我强烈要求使用 hmtlentities 或其他任何东西来保护它,因为它是直接的用户输入。

我只是将代码放入编译器中,最后您缺少一个}