如何使用php编辑/更新txt文件 [英] How to edit/update a txt file with php

查看:86
本文介绍了如何使用php编辑/更新txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我阅读了文件上的edit/update函数的许多类似问题之后,它们都没有起作用,我想寻求帮助.

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.

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

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

  1. 这是我在这里阅读的最后一个代码,但是没有用.

  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);

  • 这是我以前的尝试:

  • And this is my previous try:

    $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.

    这是我的全部代码:

    <?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

    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

    推荐答案

    您只需更改:

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

    对此:

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

    PHP中的串联运算符为.(不是+)

    The concatenation operator in PHP is . (not +)

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

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

    $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);
    

    这篇关于如何使用php编辑/更新txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆