<?php
/*$result = array(array('name'=>'中国','sex'=>'男','age'=>'11'),array('name'=>'中国2','sex'=>'男2','age'=>'21'));
$str = "姓名,性别,年龄
";
$str = iconv('utf-8','gb2312',$str);
foreach($result as $row){
$name = iconv('utf-8','gb2312',$row['name']); //中文转码
$sex = iconv('utf-8','gb2312',$row['sex']);
$str .= $name.",".$sex.",".$row['age']."
"; //用引文逗号分开
}
$filename = date('Ymd').'.csv'; //设置文件名
export_csv($filename,$str); //导出
function export_csv($filename,$data) {
header("Content-type:text/csv");
header("Content-Disposition:attachment;filename=".$filename);
header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
header('Expires:0');
header('Pragma:public');
echo $data;
}*/
$filename = date('Ymd').'.csv'; //设置文件名
$handle = fopen('./'.$filename, 'r');
$result = input_csv($handle); //解析csv
$len_result = count($result);
if($len_result==0){
echo '没有任何数据!';
exit;
}
var_dump($result);
//$data_values = substr($data_values,0,-1); //去掉最后一个逗号
fclose($handle); //关闭指针
function input_csv($handle) {
$out = array ();
$n = 0;
while ($data = fgetcsv($handle, 10000)) {
$num = count($data);
for ($i = 0; $i < $num; $i++) {
$out[$n][$i] = $data[$i];
}
$n++;
}
return $out;
}
?>