簡體   English   中英

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

[英]how to prevent php fwrite() from overwriting a line of text on file?

我從腳本中提取了以下代碼行。

    $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

從屏幕快照中可以看到,當選擇一個或多個記錄並單擊下拉按鈕時,我希望從數據庫中刪除記錄,但希望將相應的sql插入語句寫入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) 這並沒有發生。 而是先前寫入的字符串會被新的字符串覆蓋。 我已經反復嘗試過,但是只有最近的字符串會覆蓋舊的字符串。

php在手冊中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!');
}

嘗試使用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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM