逐行读取txt文件
header("Content-type:text/html; charset=utf-8"); $handle = fopen('test.php', 'r') or die('Unable to file!'); $keyarr = array(); if ($handle) { while (!feof($handle)) { $buffer = fgets($handle, 4096); $buffer = str_replace(',', ',', $buffer); $buffer = str_replace(array("\n","\r"), '', $buffer); $buffer = trim($buffer); if(!empty($buffer)) { $keyarr[] = explode(',', $buffer); } } fclose($handle); } $con = mysql_connect('localhost', 'root', '') or die('数据库连接失败:'.mysql_error()); mysql_select_db('test', $con) or die('Could not select database'); mysql_query("set names utf8"); $val = ''; foreach($keyarr as $k => $v) { // $sql = "INSERT INTO `keyword` `key_title` = {$v[0]}, `url` = {$v[1]} `num` = {$v[2]}"; $key_title = trim($v[0]); $url = trim($v[1]); $num = trim($v[2]); $val .= " ('{$key_title}', '{$url}', '{$num}'),"; } $val = rtrim($val, ','); $sql = "INSERT INTO `keyword` (`key_title`, `url`, `num`) VALUES {$val}"; $query = mysql_query($sql); if($query) { echo "文件导入成功!"; } else { echo "文件导入失败!"; } mysql_close($con);