如何防止php fwrite()覆盖文件中的一行文本?

时间:2012-02-13 14:20:22

标签: php fwrite overwrite

我从我的脚本中提取了以下代码行。

    $i=0;
    $j=0;
    $fp=fopen('C:\xampp\htdocs\Lib_auto_project\Deleted_Records\delete_log.sql','a+');// File where the string is to be written
    foreach($_POST as $temp)//repeat for all the values passed from the form
    {
        if($j==0)
          {     
               $result_set=$mysqli->query("select * from {$_SESSION['table_name_1']} where Copyid=$temp");
               $result_set=$result_set->fetch_array(MYSQL_BOTH);    
               ++$j;

           }
        if($temp!='Drop')// Drop is simply the value of submit buttom
         {
            $date=mysql_query("select now() as current_date_time") or die(mysql_error());
            $date=mysql_fetch_array($date,MYSQL_BOTH);
            $result="\n"."INSERT INTO delete_book_log  // this is the string begining with line break and followed by sql insert statement
                          VALUES(
                                 '{$result_set["Bid"]}',
                                 '$temp',
                                 '{$result_set["Name"]}',
                                 '{$result_set["Publication"]}',
                                 '{$result_set["ISBN"]}',
                                 '{$result_set["Author"]}',
                                 '{$result_set["Edition"]}',
                                 'in',
                                 '{$result_set["Book_Baseid"]}',
                                 '{$date['current_date_time']}'
                                 );";
              fflush($fp);
              fwrite($fp,$result);
              $mysqli->query("Delete from {$_SESSION['table_name_1']} where copyid=$temp");
              $i++;
          }

      }
      fclose($fp);

![截屏]:http://i.stack.imgur.com/dOzSj.jpg

从屏幕截图中可以看出,当选择了一个或多个记录并单击了drop按钮时,我希望从数据库中删除记录,但是希望将相应的sql insert语句写入一个file(C:\xampp\htdocs\Lib_auto_project\Deleted_Records\delete_log.sql)。为了实现这一点,我已经编写了上面的代码。现在的问题是当我选择记录并将它们全部删除时。当我执行相同的任何其他瞬间时,我想要类似的sql插入字符串,如上面所示存储在$ result中,附加到file(C:\xampp\htdocs\Lib_auto_project\Deleted_Records\delete_log.sql)的末尾。这不会发生。而是先前写入的字符串被新的字符串覆盖。 我一遍又一遍地尝试过,但只有最近的字符串才会覆盖旧字符串。

2 个答案:

答案 0 :(得分:1)

php在the manual中说a+(使用a对您来说应该足够了),这很好:

  

开放阅读和写作;将文件指针放在文件的末尾。如果该文件不存在,请尝试创建它。

但尝试运行此代码(称为test.php):

<?php    
$fp = fopen( 'test.php', 'a+') or die( 'Cannot open!');
echo 'Pos: ' . ftell($fp) ."\n";
echo fgets($fp);
echo 'Pos: ' . ftell($fp) ."\n";
fclose( $fp);

它会生成此输出:

Pos: 0
<?php
Pos: 6

您可以使用fseek()

fseek( $fp, 0, SEEK_END);

或使用file_put_contents()使用适当的参数:

file_put_contents( $file, $string, FILE_APPEND);

不要忘记检查您的文件权限以及是否通过以下代码成功打开文件:

if( !$fp){ 
    die( 'File cannot be opened!');
}

答案 1 :(得分:-1)

尝试使用file_get_contents和file_put_contents而不是fwrite,并将查询附加到数组或字符串:

$fp=file_get_contents('C:\xampp\htdocs\Lib_auto_project\Deleted_Records\delete_log.sql');// File where the string is to be written
foreach($_POST as $temp)//repeat for all the values passed from the form
{
    if($j==0)
      {     
           $result_set=$mysqli->query("select * from {$_SESSION['table_name_1']} where Copyid=$temp");
           $result_set=$result_set->fetch_array(MYSQL_BOTH);    
           ++$j;

       }
    if($temp!='Drop')// Drop is simply the value of submit buttom
     {
        $date=mysql_query("select now() as current_date_time") or die(mysql_error());
        $date=mysql_fetch_array($date,MYSQL_BOTH);
        $result="\n"."INSERT INTO delete_book_log  // this is the string begining with line break and followed by sql insert statement
                      VALUES(
                             '{$result_set["Bid"]}',
                             '$temp',
                             '{$result_set["Name"]}',
                             '{$result_set["Publication"]}',
                             '{$result_set["ISBN"]}',
                             '{$result_set["Author"]}',
                             '{$result_set["Edition"]}',
                             'in',
                             '{$result_set["Book_Baseid"]}',
                             '{$date['current_date_time']}'
                             );";
          $fp .= $result . "\r\n";
          $mysqli->query("Delete from {$_SESSION['table_name_1']} where copyid=$temp");
          $i++;
      }

  }
  file_put_contents($fp);