本文目录一览:
php如何读取CSV大文件并且将其导入数据库示例
思路:
读取csv文件,每读取一行数据,就插入数据库
示例
文件夹结构
/
file.csv //csv大文件,这里只模拟三行数据,不考虑运行效率(PS:csv文件格式很简单,文件一般较小,解析很快,运行效率的瓶颈主要在写入数据库操作)
index.php //php文件
file.csv
singi,20
lily,19
daming,23
index.php
/**
* 读取csv文件,每读取一行数据,就插入数据库
*/
//获取数据库实例
$dsn = 'mysql:dbname=test;host=127.0.0.1';
$user = 'root';
$password = '';
try {
$db = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
echo 'Connection failed: ' . $e-getMessage();
}
//读取file.csv文件
if (($handle = fopen("file.csv", "r")) !== FALSE) {
while (($row = fgetcsv($handle, 1000, ",")) !== FALSE) {
//写入数据库
$sth = $db-prepare('insert into test set name=:name,age=:age');
$sth-bindParam(':name',$row[0],PDO::PARAM_STR,255);
$sth-bindParam(':age',$row[1],PDO::PARAM_INT);
$sth-execute();
}
fclose($handle);
}
数据表
CREATE TABLE `test` (
`id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NULL DEFAULT '' COLLATE 'utf8mb4_bin',
`age` INT(10) NULL DEFAULT '0',
PRIMARY KEY (`id`)
)
COLLATE='utf8mb4_bin'
ENGINE=InnoDB;
运行结束后,数据库中会插入csv中的三行数据
php中如何导入导出CSV格式的文件
php导入CSV文件:
$line_number = 0;
$handle = fopen("1.xls","r");
while ($data = fgetcsv ($handle, 100000, ",")) {
if($line_number == 0){
$line_number++;
continue;
}
//z这样就可以去掉表头的那一行
for ($i = 0; $i count($data); $i++) {
$ziduan = $data[$i];
}
}
2.php导出CSV文件
header( "Cache-Control: public" );
header( "Pragma: public" );
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=txxx.csv");
header('Content-Type:APPLICATION/OCTET-STREAM');
ob_start();
$header_str = iconv("utf-8",'gbk',"信息id,标题,名称,电话,QQ,Email,内容,时间\n");
$file_str="";
$mysqli= new mysqli('localhost','root','','test');
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$sql='select * from messages';
$mysqli-query("set names utf8 ;");
$result=$mysqli-query($sql);
if($result){
while ($row = mysqli_fetch_assoc($result)){
$file_str.= $row['id'].','.$row['title'].','.$row['name'].','."'{$row['telephone']}'".','.$row['qq'].','.$row['email'].','.str_ireplace(',',',',$row['content']).','.$row['retime']."\n";
}
}else{
echo "nonono!!!";
}
$file_str= iconv("utf-8",'gbk',$file_str);
ob_end_clean();
echo $header_str;
echo $file_str;
?
php 读取同一目录下的csv文件(一个或多个)
如果你的代码是可以正常运行的,用glob函数可以读取相对应的文件
?php
$path = 'warehouse_data/DFRC/';
foreach (glob($path."*.csv") as $filename) {
$handle = fopen($filename,"r");
setlocale(LC_ALL,array('zh_CN.gbk','zh_CN.GBK GBK ','zh_CN.gb2312','zh_CN.gb18030','en_US.UTF-8','zh_CN'));
while ($data = fgetcsv($handle, 2000, ",")) {
$sql = "insert into `".$t."`(`pu_no`,`cust_no`,`custname`,`requ`,`supply`,`supply_no`,`ck_adr`,`arr_time`,`arr_adr`,`unload_point`,`indent_mount`,
`snp`,`case_mount`,`dgs`,`gj_dd`,`sd`,`pack_no`,`auto_style`) values ('".$data[0]."','".$data[1]."','".$data[2]."','".$data[3]."','".$data[4]."',
'".$data[5]."','".$data[6]."','".$data[7]."','".$data[8]."','".$data[9]."','".$data[10]."','".$data[11]."','".$data[12]."','".$data[14]."','".$data[15]."',
'".$data[16]."','".$data[17]."','".$data[29]."')" ;
echo $sql.'br /';
mysql_query($sql);
//print_r($data);
}
unset($handle);
unlink($filename);
}
?