PHP讀取CSV文件,由於Excel最多只能顯示65536行,數據量大不能完全顯示
CSV文件直接導入Mysql不好使, 於是采用PHP直接CSV文件今天發現fgetcsv()函數操作CSV文件讀取入庫
代碼還沒做整理,
代碼如下
<?php
set_time_limit(0);
$host = 'localhost';
$user = 'root';
$pswd = '';
$dbname = 'email';
$emailArr = array('admin', 'administrator', 'contact', 'info', 'support', 'webmaster');
mysql_connect($host,$user,$pswd) or die('connect error');
mysql_select_db($dbname) or die('Select db error');
function randStr($len=6) {
$chars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
mt_srand((double)microtime()*1000000*getmypid());
$password='';
while(strlen($password)<$len)
$password.=substr($chars,(mt_rand()%strlen($chars)),1);
return $password;
}
$csvFileName = '2.csv';
$row = 0;
$handle = fopen($csvFileName,"r");
while ($data = fgetcsv($handle, 1000, ",")) {
//$sql = mysql_query("INSERT INTO email SET email = '$data[1]'");
$rand = randStr(15);
$row++;
$domain = $data[1];
for ($i = 0; $i < count($emailArr); $i++) {
$email = $emailArr[$i].'@'.$domain;
$sql = mysql_query("INSERT INTO email SET email = '$email', domain = '$domain', domain_id = '$row', rand = '$rand'");
}
}
fclose($handle);
echo 'success';
exit;
$content = file_get_contents($csvFileName);
print_r($content);exit;
$content = explode("n",$content);
for ($i = 0; $i < count($content); $i++) {
print_r($content);exit;
$randStr = '';
foreach ($content as $value) {
$randStr = randStr(6);
$mail = explode(",",$value);
$companyArr = explode(".",$mail[1]);
$companyName = $companyArr[0];
//echo $companyName;exit;
for ($j = 0; $j < count($emailArr); $j++) {
$email = $emailArr[$j]."@".$mail[1];
echo $email.'<br />';
echo $randStr.'<br />';
echo $companyName.'<br />';
//$sql = mysql_query("INSERT INTO mail_source SET mail = '$email'");
}
}
exit;
}
?>