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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18865548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:20:47  来源:igfitidea点击:

How to edit/update a txt file with php

php

提问by Mr Andreev

After I've read a lot of similar problems with the edit/update function on a file and none of it worked I would like to ask for some help.

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

I am trying to edit a .txtdocument from php. I have tried these things:

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

  1. 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);
    
  2. And this is my previous try:

    $new_contents = "$_POST[subject]\n";
    $path = "text/$row[name]";
    file_put_contents($path, $new_contents);
    
  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);
    

I hope someone would explain me how to do this the right way. Thank you.

我希望有人能解释我如何以正确的方式做到这一点。谢谢你。

This is all of my code:

这是我所有的代码:

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

Just to add something which might be useful:
I am getting this error which I can't manage to find an answer for with Google.
Warning: fopen(text/) [function.fopen]: failed to open stream: Is a directory in

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

回答by Nikhil Patel

You just need to change :

你只需要改变:

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

to this :

对此:

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

The concatenation operator in PHP is .(not +)

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

And make sure the directory textexists, otherwise its better to check and then write :

并确保目录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);

回答by Ankur Kumar Singh

You can also open file in append mode using fopen() and put whatever you have at the end like

您还可以使用 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);

Detail is mentioned in TechFlirt File Handling Bloghere

此处的TechFlirt 文件处理博客中提到了详细信息

回答by Moeed Farooqui

You need to use file_get_contentsto get the text of your file.

您需要使用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);

See Documentation

查看文档

回答by Moeed Farooqui

I just put the code into a compiler, you are missing a }at the end.

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

回答by user2781686

I think the problem may be the first line :

我认为问题可能出在第一行:

$data_to_write = "$_POST[subject]";

Replace it with the following :

将其替换为以下内容:

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

And i highly recommand to protect this with hmtlentities or anything else as it's a direct user input.

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