首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我需要写在文件的开头与PHP

我需要写在文件的开头与PHP
EN

Stack Overflow用户
提问于 2009-11-19 10:30:50
回答 9查看 57.3K关注 0票数 42

我正在编写这个程序,我试图找出如何将数据写入文件的开头,而不是结尾。"a"/append只写到结尾,我怎么才能让它写到开头呢?因为"r+“会这样做,但会覆盖以前的数据。

代码语言:javascript
运行
复制
$datab = fopen('database.txt', "r+");

下面是我的整个文件:

代码语言:javascript
运行
复制
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>Facebook v0.1</title>
        <style type="text/css">
            #bod{
                margin:0 auto;
                width:800px;
                border:solid 2px black;
            }
        </style>
    </head>

    <body>
        <div id="bod">
            <?php
                $fname = $_REQUEST['fname'];
                $lname = $_REQUEST['lname'];
                $comment = $_REQUEST['comment'];
                $datab = $_REQUEST['datab'];
                $gfile = $_REQUEST['gfile'];

                print <<<form
                <table border="2" style="margin:0 auto;">
                    <td>
                        <form method="post"  action="">
                              First Name :
                              <input type ="text"
                                         name="fname"
                                         value="">
                              <br>

                              Last Name :
                                <input type ="text"
                                         name="lname"
                                         value="">
                              <br>

                              Comment :
                              <input type ="text"
                                         name="comment"
                                         value="">
                              <br>
                              <input type ="submit" value="Submit">
                        </form>
                    </td>
                </table>
                form;
                if((!empty($fname)) && (!empty($lname)) && (!empty($comment))){
                    $form = <<<come

                    <table border='2' width='300px' style="margin:0 auto;">
                        <tr>
                            <td>
                                <span style="color:blue; font-weight:bold;">
                                $fname $lname :
                                </span>

                                $comment
                            </td>
                        </tr>
                    </table>
                    come;

                    $datab = fopen('database.txt', "r+");
                    fputs($datab, $form);
                    fclose($datab);

                }else if((empty($fname)) && (empty($lname)) && (empty($comment))){
                    print" please input data";
                } // end table

                $datab = fopen('database.txt', "r");

                while (!feof($datab)){
                    $gfile = fgets($datab);
                    print "$gfile";
                }// end of while
            ?>
        </div>
    </body>
</html>
EN

回答 9

Stack Overflow用户

发布于 2009-11-19 10:36:17

快速和肮脏:

代码语言:javascript
运行
复制
<?php
$file_data = "Stuff you want to add\n";
$file_data .= file_get_contents('database.txt');
file_put_contents('database.txt', $file_data);
?>
票数 66
EN

Stack Overflow用户

发布于 2009-11-19 10:46:25

如果不想将文件的全部内容加载到一个变量中,可以使用PHP的Streams特性:

代码语言:javascript
运行
复制
function prepend($string, $orig_filename) {
  $context = stream_context_create();
  $orig_file = fopen($orig_filename, 'r', 1, $context);

  $temp_filename = tempnam(sys_get_temp_dir(), 'php_prepend_');
  file_put_contents($temp_filename, $string);
  file_put_contents($temp_filename, $orig_file, FILE_APPEND);

  fclose($orig_file);
  unlink($orig_filename);
  rename($temp_filename, $orig_filename);
}

这样做的目的是将要预先添加的字符串写入临时文件,然后将原始文件的内容写入临时文件的末尾(使用streams而不是将整个文件复制到变量中),最后删除原始文件并重命名临时文件以替换它。

注意:这段代码最初是基于Chao Xu的一篇现已停刊的博客文章。代码后来有所不同,但最初的帖子可以在in the Wayback Machine上查看。

票数 45
EN

Stack Overflow用户

发布于 2017-05-15 03:40:59

  1. w+模式而不是a+模式打开文件。如果需要,
  2. 获取用于将文件光标添加到文件开头的文本长度
  3. 从文件读取$chunkLength字节<代码>H212<代码>H113将光标返回到步骤4中的值<代码>H221<代码>H122执行这些步骤,<代码>D23

$handler = fopen('1.txt','w+');//1倒带($handler);//3 $prepend =“我想将此文本添加到此文件的开头”;$chunkLength = strlen( $prepend );//2 $i = 0;do{ $readData = fread($handler,$chunkLength);//4 fseek($handler,$i * $chunkLength);//5 fwrite($handler,$prepend);//6 $prepend= $readData;//7 $i++;}while ($readData);//8 fclose($handler);

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1760525

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档