我正在编写这个程序,我试图找出如何将数据写入文件的开头,而不是结尾。"a"/append只写到结尾,我怎么才能让它写到开头呢?因为"r+“会这样做,但会覆盖以前的数据。
$datab = fopen('database.txt', "r+");下面是我的整个文件:
<!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>发布于 2009-11-19 10:36:17
快速和肮脏:
<?php
$file_data = "Stuff you want to add\n";
$file_data .= file_get_contents('database.txt');
file_put_contents('database.txt', $file_data);
?>发布于 2009-11-19 10:46:25
如果不想将文件的全部内容加载到一个变量中,可以使用PHP的Streams特性:
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上查看。
发布于 2017-05-15 03:40:59
w+模式而不是a+模式打开文件。如果需要,$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);
https://stackoverflow.com/questions/1760525
复制相似问题