PHP脚本处理文件的所有行两次

时间:2013-10-31 19:57:01

标签: php mysql

我有一个PHP脚本,可以生成一个HTML表单供用户上传文件。我使用move_uploaded_file将该文件保存在服务器上,然后使用fgets()读取它,并根据特定检查执行数据库插入。这是代码的简化版本:

$cart_id = 18566;
if (empty($_POST))
{
  echo "<form name=\"upload\" action=\"myscan.php\" id=\"myScan\" method=\"POST\"  enctype=\"multipart/form-data\">";
  echo "Choose the file to upload:<br>\r\n";
  echo "<input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"300000\" form=\"myScan\" />";
  echo "<input type=\"file\" name=\"file\" form=\"myScan\" id=\"fileUp\"  /><br>";
  echo "<input type=\"submit\" value=\"Upload\" name=\"sub\" form=\"myScan\" />";
  echo "<input type=\"hidden\" name=\"ck\" form=\"myScan\" value=\"".$cart_id."\" />";
  echo "</form><br>";
}
else
{
  // link to cart goes here
}

$fname = "1SCAN20131031123456";
if (!empty($_POST))
{
  $allowedExts = array("txt", "csv");
  $extension = end(explode(".", $_FILES["file"]["name"]));
  if ( ($_FILES["file"]["type"] == "text/plain" 
      || $_FILES["file"]["type"] == "application/vnd.ms-excel")
    && array_search(strtolower($extension), array_map('strtolower', $allowedExts)) !== FALSE )
  {
    if ($_FILES["file"]["error"] > 0)
    {
      echo "Error: " . $_FILES["file"]["error"] . "<br>";
    }
    else
    {
      echo "Upload: " . $_FILES["file"]["name"] . "<br>";
      echo "Type: " . $_FILES["file"]["type"] . "<br>";
      echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
      echo "Stored in: " . $_FILES["file"]["tmp_name"]."<br>";
      if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
        echo $_FILES["file"]["name"] . " already exists. ";
      }
      else
      {
        move_uploaded_file($_FILES["file"]["tmp_name"], $fname);
        echo "Moved to: " . $fname . "<br>";
      }
    }
  }
  else
  {
  echo "Invalid file<br>";
  echo "Type: " . $_FILES["file"]["type"] . "<br>";
  echo "Upload: " . $_FILES["file"]["name"] . "<br>";
  echo "Extension: " . $extension . "<br>\n";
}

此时文件$fname没问题,没有重复的行。下一部分被称为在同一PHP文件中声明的函数。 $link$fname$cart_id被声明为全局

$bn = basename($fname);
$sfd = fopen($fname, "r");
$store_number = "$bn[0]";
if(is_numeric($bn[1]))
  $store_number .= $bn[1];
$a = stripos($bn, "SCAN");
$a += 4;
$dt = substr($bn, $a);
//  echo "Date = $dt \n";
$fy = substr($dt, 0, 4);
$fM = substr($dt, 4, 2);
$fd = substr($dt, 6, 2);
$fh = substr($dt, 8, 2);
$fm = substr($dt, 10, 2);
$fs = substr($dt, 12, 2);
$fdate = "$fy-$fM-$fd $fh:$fm:$fs";
//  echo $fname . ",";
//  echo $store_number . ",";

while ($line = fgets($sfd))
{
  $li = explode(",", $line);
  if (sizeof($li) == 5)
  {
    $scan = $li[0];
    $poQty = $li[1];
    $cntQty = $li[2];
    $limd = $li[3];
    $lihms = $li[4];
    $query = "INSERT INTO upload_datalog (file_name, store, filedate, scan, po_qty, cnt_qty, scan_md, scan_hms, cart_id)\n"
    . "VALUES (\"$bn\", $store_number, \"$fdate\", \"$scan\", $poQty, $cntQty, \"$limd\", \"$lihms\", $cart_id)";
  mysqli_query($link, $query);
  }
  else if ($fM < 8 || ($fM == 8 && $fd < 16 ))
  {
    $scan = $li[0];
    $poQty = $li[2];
    $cntQty = $li[1];
    $limd = $li[3];
    $lihms = $li[4];
    $query = "INSERT INTO upload_datalog (file_name, store, filedate, scan, po_qty, cnt_qty, scan_md, scan_hms, cart_id)\n"
    . "VALUES (\"$bn\", $store_number, \"$fdate\", \"$scan\", $poQty, $cntQty, \"$limd\", \"$lihms\", $cart_id)";
    mysqli_query($link, $query);
  }
  else if (sizeof($li) == 3 && $li[0] != "" && $li[1] != "" &&$li[2] != "" )
  {
    $scan = $li[0];
    $poQty = $li[2];
    $cntQty = $li[1];
    $query = "INSERT INTO upload_datalog (file_name, store, filedate, scan, po_qty, cnt_qty)\n"
    . "VALUES (\"$bn\", $store_number, \"$fdate\", \"$scan\", $poQty, $cntQty)";
    mysqli_query($link, $query);
  }
}
fclose($sfd);

该文件包含以下信息:

这一切都超过99%的时间,但在过去两周内两次,upload_datalog表中的条目已被复制。此后还有另一个函数,它还会读取文件并根据不同的检查执行插入,并且这些函数也是重复的。

我知道这是一个边缘案例,但我找不到任何关于为什么会在php.net或谷歌上发生这种情况的信息,而且我无法重现它mysqlf。但我知道它发生在野外。

我在这里看不到竞争条件吗?

1 个答案:

答案 0 :(得分:1)

你在while循环中进行插入,所以你在那个循环中运行两次? 对于调试,在循环中添加一个计数器并创建一个小的调试函数和一个调试表。将计数器和数据插入调试表中。将这些值与您的期望进行比较