数据库中设置好一个表txt,包含五个字段。
txt (id, txt1, txt2, txt3, txt4)Txt文件中包含的内容是也是5个[33,1,"中国","陕西","宝鸡"]1000多的文件已获得文件名主名:pageID[0]占位
$pageID =array(0,1970634101,1987477110,……………………);如何用PHP把1000多个.txt插入到MYSQL数据库中。

解决方案 »

  1.   

    <?php 
    if ($handle = opendir('D:\\a\\')) //设置文件路径
    {
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") { //读取目录下的文件
      
     
    $lines = file('D:\\a\\'.$file);
    foreach ($lines as  $fline) 
    {
                   $newfile= explode(',', $fline); //分割后存入数组
       // insert into table() value($newfile[0]...$newfile[4]);
                }
            }
        }
        closedir($handle);
    }
    ?> 
      

  2.   

    说个思路吧,用pHP打开文件夹,打开第一个文档,遍历,读取进数据库,完成,将文档移动到另一个文件夹,依次下去直到这个文件夹里为空。
      

  3.   

    樓上的"将文档移动到另一个文件夹"這一步是多餘的,這只會讓原本就要執行很長時間的程式更耗時間.有時還會出現內存不足的問題這個問題也沒什麼難的,樓上二位都已經給出了代碼和思路
    超時的問題,只要讓程式無限時進行,或直接php.exe phpfile就行了
    內存問題的話,只能看你的機器了.如果可以的話,建議你在服務器上進行
      

  4.   

    <?php header('Content-Type: text/html; charset=utf8'); $con = mysql_connect("localhost","root","root");
    mysql_query("set names 'utf8'");if (!$con)
      {
      die('不能连接到数据库: ' . mysql_error());
      }mysql_select_db("Exam", $con);if ($handle = opendir('ExamBase')) //设置文件路径{
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") { //读取目录下的文件
              
             
                $lines = file('ExamBase/'.$file);
                foreach ($lines as  $fline) 
                {
         
     $b = str_replace("[","",$fline);
     $a = str_replace("]","",$b);
     $c = str_replace("\"","", $a);  
               $newfile= explode(',', $c); //分割后存入数组

    $sql="INSERT INTO myExam(myExamID,myExamTx, myExamCnt, myExamImg, myExamDaRight) values ('$newfile[0]','$newfile[1]','$newfile[2]','$newfile[3]','$newfile[4]')";
      if (!mysql_query($sql,$con))
      {
      die('错误: ' . mysql_error());
      }
               }
            }
        }
        closedir($handle);
    }
    mysql_close($con)
    ?>