PHP脚本删除.dat文件中的行

时间:2014-08-01 22:21:59

标签: php database

我从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 

1 个答案:

答案 0 :(得分:0)

似乎更新的条目通过在!前加上来标记。此循环似乎只保存更新,而不是每个记录。

可能存在拼写错误 - fopen模式"w"将在写入前将数据文件截断为0字节,如果只写入更新的记录,则不应该是模式{{ 1}}将它们附加到现有文件?

对数据文件允许增长的大小也有限制,请确保将其设置得足够大以容纳1000条记录。