요건 임포트 하는 코드 ㅋㅋ
【PHP】 CSVをいい感じにインポートする方法を紹介します
$filepath = "../file/test.csv";
$file = new SplFileObject($filepath);
$file->setFlags(SplFileObject::READ_CSV);
$ins_values = "";
foreach ( $file as $key => $line ) {
$judge = count( array_count_values( $line ) );
if( $judge == 0 ){
continue;
}
$values = "";
foreach ( $line as $line_key => $str ) {
if( $line_key > 0 ){
$values .= ", ";
}
$values .= "'".mb_convert_encoding( $str, "utf-8", "sjis" )."'";
}
if( !empty( $ins_values ) ){
$ins_values .= ", ";
}
$ins_values .= "(". $values . ")";
}
$sql_insert = "INSERT INTO テーブル名 ( カラム01, カラム02, カラム03 ) VALUES " . $values;
mysql_query( $sql_insert, $connect );
요건 엑스포트 하는 코드 ㅋㅋ
【PHP】データをCSVでいい感じにエクスポートする方法
【コード】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
$file_path = "export.csv";
$export_csv_title = array( "id", "氏名", "性別", "電話番号" );
$sql_export = "SELECT id, 氏名, 性別, 電話番号 FROM table1 ";
$res_export = mysql_query( $sql_export, $connect );
if( touch($file_path) ){
$file = new SplFileObject( $file_path, "w" );
foreach( $export_csv_title as $key => $val ){
$export_header[] = mb_convert_encoding($val, 'SJIS-win', 'UTF-8');
}
$file->fputcsv($export_header);
while( $row_export = mysql_fetch_assoc( $res_export ) ){
$export_arr = "";
foreach( $row_export as $key => $val ){
$export_arr[] = mb_convert_encoding($val, 'SJIS-win', 'UTF-8');
}
$file->fputcsv($export_arr);
}
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\\"".basename($file_path)."\\";" );
header("Content-Transfer-Encoding: binary");
readfile("$file_path");
|
댓글
댓글 쓰기