从文本文件将信息插入SQL数据库

时间:2013-11-23 14:03:32

标签: php mysql

我有一些代码可以使用逗号作为分隔符的大型文本文件,并将其输入到数据库中。

我收到了代码的fget和feof部分的错误。该文件已成功上传。

它说“feof():提供的参数不是有效的流资源......”和“fgets():提供的参数不是有效的流资源......”

 if(isset($_POST['submit'])){
$allowedExts = "txt";
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);

  if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_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 "Temp file: " . $_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"],
  "upload/" . $_FILES["file"]["name"]);
  echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
  }
}



$tableArray = array();
$handle = @fopen("/uploads/". $_FILES["file"]["name"], "r");

require ('connect.php');

while (!feof($handle)) // Loop til end of file.
{
    $buffer = fgets($handle, 4096); // Read a line.
    list($fname,$lname,$snumber,$gender,$a , $b, $year,$DOB, $phone,$c,$email,$d,$e,$R,$r,$w)=explode(",",$buffer);
    $sql_statement = "INSERT INTO students  VALUES ($snumber , $fname , $lname , $gender , $year , $email, $DOB , $phone , 1)";

}


}

1 个答案:

答案 0 :(得分:0)

您可以对此

使用LOAD DATA INFILE查询

您可以在此处找到文档

http://dev.mysql.com/doc/refman/5.1/en/load-data.html

要打开文件,请使用:

$handle = fopen("<file location>","r");