0

我有一个 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% 的时间内都有效,但在过去 2 周内,表中的条目upload_datalog有两次重复。在此之后还有另一个函数,它也读取文件并根据不同的检查执行插入,并且这些也是重复的。

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

有没有我在这里没有看到的比赛条件?

4

1 回答 1

1

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

于 2013-10-31T20:10:19.510 回答