簡體   English   中英

PHP-Script刪除.dat文件中的行

[英]PHP-Script deletes lines in .dat file

自9個小時以來,我一直在努力解決以下問題,並且找不到解決方案。 我正在使用來自此處的非常舊的腳本( http://www.teralaser.net/invitephp/ )構建邀請腳本。 我已經在過去一周內完成了所有樣式,現在看起來很棒。 該腳本將單個數據集存儲在.dat文件中。 現在我意識到,它始終只保留數據庫的前125行,並刪除其余的行。 這是他的一個擁抱,因為要邀請近1000人參加。 幾個小時后,我發現(最好是我猜到了)這是下面的代碼行,它摧毀了它:

case "adminadd":
  if (isset($_POST["pwd"])) $pwd = $_POST["pwd"];
  else $pwd="";
  $pwdchk = 1;
  if (isset($_GET["pwd2"])) $pwdchk = strcasecmp(md5( date("z") . $admin_password),$_GET["pwd2"]);

  if (strcasecmp($pwd,$admin_password) != 0 && $pwdchk != 0) {
         echo "<HTML><HEAD><META HTTP-EQUIV='REFRESH' CONTENT='1; URL=$PHP_SELF?do=admin'></HEAD><BODY bgcolor='#ffffff'><CENTER><H2>Falsches Passwort!</H2>Bitte warten...</CENTER></BODY><HTML>";
         exit;
  }

  // Get all administrator changes
  $record = file($data_file);
  rsort($record);
  $jmlrec = count($record);


  // Delete duplicates (the dum way...) and get host email if any
  $hostemail = "";
  for ($i = 0; $i < $jmlrec; $i++) {
    if (isset($record[$i])) {
      $r = $record[$i];
      $row = explode("|~~|",$r);
      $chkmail = $row[2];
      if ($row[4] == "Gastgeber") $hostemail = $row[2];
      for($j = $i+1; $j < $jmlrec; $j++) {
        $r = $record[$j];
        $row = explode("|~~|",$r);        
        if (strcasecmp($row[2],$chkmail)==0 ) { $record[$j] = ""; }
      }
    }
  }

  $updno = 0;
  $err = 0;
  for ($i = 0; $i < $jmlrec; $i++) { // Geändert von $jmlrec+12
    // Changes ?
    if (isset($_POST["vemail".$i])) $chkmail = $_POST["vemail".$i]; else $chkmail ="";
    $found = -1;
    if ($chkmail != "") {
      // Find existing record if possible...
      $recno = count($record);
      for($j = 0; $j < $recno; $j++) {
        $r = $record[$j];
        $row = explode("|~~|",$r);
        if (strcasecmp($row[2],$chkmail) == 0) { $found = $j; break; }
      }
      $changed = 1;
      if ($found >= 0) {
        // Changed ?
        if ( $row[2] == $_POST["vemail".$i] && $row[3] == $_POST["vname".$i] && $row[4] == $_POST["vreply".$i] && $row[5] == $_POST["vamount".$i] &&
             $row[6] == $_POST["vcompany".$i] && $row[7] == $_POST["vcomment".$i] ) $changed = 0;
      }
      if ( $changed != 0 ) {
        $updno++;
        $idx = date("YmdHis");
        $tgl = date("d.m.Y H:i");

        $vemail = str_replace("<","",$_POST["vemail".$i]);
        $vemail = str_replace(">","",$vemail);
        $vemail = str_replace("~~","--",$vemail);
        $vemail = str_replace("\"","&quot;",$vemail);

        $vname = str_replace("<","",$_POST["vname".$i]);
        $vname = str_replace(">","",$vname);
        $vname = str_replace("~","-",$vname);
        $vname = str_replace("\"","&quot;",$vname);

        $vreply = str_replace("<","",$_POST["vreply".$i]);
        $vreply = str_replace(">","",$vreply);
        if ($vreply != "ungesehen" && $vreply != "gesehen" && $vreply != "unentschlossen" && $vreply != "abgesagt" && $vreply != "zugesagt" && $vreply != "Gastgeber") $vreply = "ungesehen";
        if ($vreply != "Gastgeber" && $vreply != "ungesehen" && $found >= 0 ) $tgl = $tgl . "*"; // admin change

        $vamount = str_replace("<","&lt;",$_POST["vamount".$i]);
        $vamount = str_replace(">","&gt;",$vamount);
        $vamount = str_replace("~","-",$vamount);
        $vamount = str_replace("\"","&quot;",$vamount);

        $vcomment = str_replace("<","&lt;",$_POST["vcomment".$i]);
        $vcomment = str_replace(">","&gt;",$vcomment);
        $vcomment = str_replace("~","-",$vcomment);
        $vcomment = str_replace("\"","&quot;",$vcomment);

        $vcompany = str_replace("<","&lt;",$_POST["vcompany".$i]);
        $vcompany = str_replace(">","&gt;",$vcompany);
        $vcompany = str_replace("~","-",$vcompany);
        $vcompany = str_replace("\"","&quot;",$vcompany);

        if (strtoupper($os) == "WIN") {
       $vcomment = str_replace("\r\n","<BR>",$_POST["vcomment".$i]);
       $vcomment = str_replace("\r","",$vcomment);
       $vcomment = str_replace("\n","",$vcomment);
        } else {
       $vcomment = str_replace("\n","<BR>",$vcomment);
       $vcomment = str_replace("\r","",$vcomment);
        }
        $validemail = 1;
        if (!preg_match("/([\w\.\-]+)(\@[\w\.\-]+)(\.[a-z]{2,4})+/i", $vemail)) $validemail = 0;
        if ( $vname != "" && $validemail == 0 ) { echo "Email not valid for $vname : $vemail <br>"; $err++; }
        if ( $vname != "" && $validemail == 1 ) {
          if ($found < 0 ) { $record[] = "\n"; $found = count($record) - 1; }
          $newdata = "<?//|~~|$idx|~~|$vemail|~~|$vname|~~|$vreply|~~|$vamount|~~|$vcompany|~~|$vcomment|~~|$tgl|~~|?>\n";
          $newdata = "!" . stripslashes($newdata);
          $record[$found] = $newdata;
        } else { 
         if ( $vname != "") { $updno--; $changed = 0; }
        }
      }
      if ( $changed == 0 ) {
        if ($found >= 0) $record[$found] = "!".$record[$found];
      }
    }
  }

  // Quick add
  $quickadd = "";
  if (isset($_POST["quickadd"]) && $_POST["quickadd"] != "") $quickadd = stripslashes($_POST["quickadd"]);
  if (trim($quickadd) != "") {
    $qadd = explode("\"",$quickadd);
    $qaddn = count($qadd);
    $nametoadd = "";
    $companytoadd = "";


    for($i = 0; $i < $qaddn; $i++) {
      if ($i % 2 == 0) {
        $q2 = explode(",",$qadd[$i]);
        $q2n = count($q2);
        for($j = 0; $j < $q2n; $j++) {
          $mailtoadd = trim($q2[$j]);
          $mailtoadd = str_replace("<","",$mailtoadd);
          $mailtoadd = str_replace(">","",$mailtoadd);
          $mailtoadd = str_replace("~~","--",$mailtoadd);
          if ($mailtoadd != "") {
            $validemail = 1;
            if (!preg_match("/([\w\.\-]+)(\@[\w\.\-]+)(\.[a-z]{2,4})+/i", $mailtoadd)) $validemail = 0;
            if ($validemail == 0 ) { echo "Fehler bei $mailtoadd (Keine g&uuml;ltige eMail-Addresse)"; $err++; } else {
              if ($nametoadd == "") { $nametoaddarr = explode("@",$mailtoadd); $nametoadd = $nametoaddarr[0]; }
              $idx = date("YmdHis");
              $newdata = "<?//|~~|$idx|~~|$mailtoadd|~~|$nametoadd|~~|ungesehen|~~|1|~~|$companytoadd|~~||~~|?>\n";
              $newdata = "!" . stripslashes($newdata);
              $record[]= $newdata;
              $nametoadd = "";
              $companytoadd = "";
            }
          }
        }
      }
      if ($i % 2 == 1) {
        $data = $qadd[$i];
        list($nametoadd, $companytoadd) = explode(";", $data);

        $nametoadd = str_replace("<","" ,$nametoadd);
        $nametoadd = str_replace(">","" ,$nametoadd);
        $nametoadd = str_replace("~","-",$nametoadd);

        $companytoadd = str_replace("|","" ,$companytoadd);
        $companytoadd = str_replace("<","" ,$companytoadd);
        $companytoadd = str_replace(">","" ,$companytoadd);
        $companytoadd = str_replace("~","-",$companytoadd);
      }

    }
  }


/* Hier liegt der tote Fuchs begraben!!! */

  $jmlrec = count($record);

  $update_data = fopen($data_file,"w");


  if (strtoupper($os) == "UNIX") {
     if (flock($update_data,LOCK_EX)) {
        for ($j=0; $j<$jmlrec; $j++) {
             if (strncasecmp($record[$j],"!",1) != 0 ) $updno++;
             if ($record[$j] != "" && strncasecmp($record[$j],"!",1) == 0 ) fputs($update_data,substr($record[$j],1));
     }
     flock($update_data,LOCK_UN);
    }
   } else {

       for ($j=0; $j<$jmlrec; $j++) {
             if (strncasecmp($record[$j],"!",1) != 0 ) $updno++;
             if ($record[$j] != ""  && strncasecmp($record[$j],"!",1) == 0 ) fputs($update_data,substr($record[$j],1));

     }
   }
   fclose($update_data);

我期待一些想法。 我很想邀請您喝咖啡...;)如果有人感興趣,我也可以發送整個腳本。

最好!

菲利普

編輯:我仍然完全迷失在代碼中,所以我想與您共享代碼的其他部分,也許有人發現問題。 ;) 謝謝!

$jml_page = intval($jmlrec/$max_entry_per_page);
                                        $sisa = $jmlrec%$max_entry_per_page;
                                        if ($sisa > 0) $jml_page++;
                                        // Set $nomaybe, $nono, $noyes DEBUG
                                        $nomaybe = 0;
                                        $nono = 0;
                                        $noyes = 0;
                                        $countmaybe = 0;
                                        $countno = 0;
                                        $countyes = 0;
                                        $counttotal = 0;
                                        $no = 0;

                                        if ($jmlrec == 0) echo "<tr><td align='center'>Noch keine Einträge.</td></tr>";

                                          $w = 0; //--Color

                                          for ($i=0; $i<$max_entry_per_page; $i++) {
                                                  // Find the lowest next possible record.
                                                  $no = $jmlrec + 1;
                                          if ($nomaybe < $jmlrec   ) $no = $nomaybe + 1;
                                          if ($nono    < $no - 1   ) $no = $nono + 1;
                                          if ($noyes   < $no - 1   ) $no = $noyes + 1;
                                                  // Check this is valid.
                                                  do {
                                                    while (($no < $jmlrec + 1) && (!isset($record[$no-1]) || $record[$no-1] == "")) { $no++; }
                                              $recno = $no - 1;
                                                    $cont = 0;
                                                    if ($no < $jmlrec + 1) {
                                                      $row = explode("|~~|",$record[$recno]);
                                                      $vr = $row[4];
                                                      $cont = 0;
                                                      if (($vr == "ungesehen" || $vr == "gesehen" || $vr == "unentschlossen") && ($no <= $nomaybe)) $cont = 1;
                                                      if (($vr == "abgesagt" ) && ($no <= $nono)) $cont = 1;
                                                      if (($vr == "zugesagt" || $vr == "Gastgeber") && ($no <= $noyes)) $cont = 1;
                                                      if ($cont == 1) $no++;
                                                    }
                                                  } while ($cont == 1);

                                          if (isset($record[$recno]) && $record[$recno] != "") {
                                           if ($w==0) { 
                                             $warna = $table_content_1a;
                                             $warna2 = $table_content_1b;
                                             $w=1;
                                           } else { 
                                             $warna = $table_content_2a;
                                             $warna2 = $table_content_2b;
                                             $w=0;
                                           }



                                      do {
                                         $nomaybe++;
                                         $recno = $nomaybe-1;
                                         $row = explode("|~~|",$record[$recno]);
                                       } 

                                       while ($nomaybe <= $jmlrec && $row[4] != "ungesehen" && $row[4] != "gesehen" && $row[4] != "unentschlossen" );

                                       if ($row[4] == "ungesehen" || $row[4] == "gesehen" || $row[4]=="unentschlossen") { 
                                         echo "<tr><td>$row[3]</td>";
                                          if (isset($row[2]) && $row[2] != "" ) echo "<td><p>$row[2]</p></td>"; else echo "<td><p></p></td>";
                                         if (isset($row[6]) && $row[6] != "" ) echo "<td><p>$row[6]</p></td>"; else echo "<td><p></p></td>";
                                         $countmaybe++;
                                       }


                                       if ($row[4] == "gesehen") echo "<td><b>gesehen</b></td><td>$row[8]</td>";
                                       if ($row[4] == "unentschlossen") echo "<td><b>unentschlossen</b></td><td>$row[8]</td>";
                                       if ($row[4] == "ungesehen") echo "<td><b>ungesehen</b></td><td></td>";
                                       echo "<td><span class='date'><b> </b></span></td>";
                                       if (isset($row[7]) && ($row[4]=="gesehen" || $row[4]=="unentschlossen" || $row[4]=="ungesehen" ) && $row[7] != "") 
                                        echo "<td><span class='comment'><i>$row[7]</i></span></td></tr>"; else echo "<td><span class='comment'></span></td></tr>";



                                      do {
                                         $nono++;
                                         $recno = $nono-1;
                                         $row = explode("|~~|",$record[$recno]);
                                       } 

                                       while ($nono <= $jmlrec && $row[4] != "abgesagt" && $row != "gesehen" && $row[5] != "unentschlossen" );
                                       $row = explode("|~~|",$record[$recno]);

                                       if ($row[4] == "abgesagt" ) { echo "<tr><td>$row[3]</td><td><p>$row[6]</p></td><td><b>abgesagt</b></td><td>$row[8]</td>";
                                         echo "<td><span class='date'><b> </b></span></td>";
                                         if (isset($row[7]) && $row[7] != "") echo "<td><span class='comment'><i>$row[7]</i></span></td></tr>"; else echo "<td><span class='comment'></span></td></tr>";
                                         $countno++;
                                       }


                                       do {
                                         $noyes++;
                                         $recno = $noyes-1;
                                         $row = explode("|~~|",$record[$recno]);
                                       } while ($noyes <= $jmlrec && $row[4] != "zugesagt" && $row[4] != "Gastgeber" );
                                       if ($row[4] == "zugesagt" || $row[4] == "Gastgeber" ) { 
                                         echo "<tr><td>$row[3]";
                                         if ($row[4] == "Gastgeber") print " (Gastgeber) ";
                                         echo "</td>";
                                        if (isset($row[6]) && $row[6] != "" ) echo "<td><p>$row[6]</p></td>"; else echo "<td><p></p></td>";
                                         echo "<td><b>zugesagt</b></td><td>$row[8]</td>";
                                        if (isset($row[5]) && $row[5] != "") echo "<td><p>$row[5]</p></td>"; else echo "<td><p>1</p></td>";

                                         if (isset($row[7]) && $row[7] != "" ) echo "<td><span class='comment'><i>$row[7]</i></span></td></tr>"; else echo "<td><span class='comment'></span></td></tr>";

                                         $countyes++;
                                         if (isset($row[5]) && $row[5] != "") $counttotal = $counttotal + intval($row[5]);
                                       }


                                         } //--end if  
                                         } //--end for 

看起來,已更新的條目前面帶有!標記! 該循環似乎僅保存更新,而不是每條記錄。

也許有錯別字-模式為"w" fopen會在寫入之前將數據文件截斷為0個字節,如果只寫入更新的記錄,那不應該是模式"a"將它們附加到現有文件上嗎?

數據文件允許增長的大小可能也有限制,請確保將其設置為足以容納1000條記錄的大小。

暫無
暫無

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

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