繁体   English   中英

如何使用 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 或其他任何东西来保护它,因为它是直接的用户输入。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM