一次性读取csv文件内所有行的数据
<?php <BR>$file = fopen('windows_2011_s.csv','r'); <BR>while ($data = fgetcsv($file)) { //每次读取CSV里面的一行内容<BR>//print_r($data); //此为一个数组,要获得每一个数据,访问数组下标即可<BR>$goods_list[] = $data;<BR> }<BR>//print_r($goods_list);<BR>/* foreach ($goods_list as $arr){<BR> if ($arr[0]!=""){<BR> echo $arr[0]."<br>";<BR> }<BR>} */<BR> echo $goods_list[2][0];<BR> fclose($file);<BR>?><BR>
读取csv文件的某一行数据
<?php<BR>function get_file_line( $file_name, $line ){<BR> $n = 0;<BR> $handle = fopen($file_name,'r');<BR> if ($handle) {<BR> while (!feof($handle)) {<BR> ++$n;<BR> $out = fgets($handle, 4096);<BR> if($line==$n) break;<BR> }<BR> fclose($handle);<BR> }<BR> if( $line==$n) return $out;<BR> return false;<BR>}<BR>echo get_file_line("windows_2011_s.csv", 10);<BR>?><BR>
读取csv文件制定行数(行区间)
<?php<BR>function get_file_line( $file_name, $line_star, $line_end){<BR> $n = 0;<BR> $handle = fopen($file_name,"r");<BR> if ($handle) {<BR> while (!feof($handle)) {<BR> ++$n;<BR> $out = fgets($handle, 4096);<BR> if($line_star <= $n){<BR> $ling[] = $out;<BR> }<BR> if ($line_end == $n) break;<BR> }<BR> fclose($handle);<BR> }<BR> if( $line_end==$n) return $ling;<BR> return false;<BR>}<BR>$aa = get_file_line("windows_2011_s.csv", 11, 20); //从第11行到第20行<BR>foreach ($aa as $bb){<BR> echo $bb."<br>";<BR>}<BR>?><BR>
另外从网上找的两种方法(没测试,不知道好不好使)
<?<BR>$handle=fopen("1.csv","r");<BR>while(!feof($handle)){<BR>$buffer=fgetss($handle,2048);<BR>$data=explode(",",$buffer);<BR>$num=c<mark>(本文来)源gaodaimacom搞#^代%!码&网(</mark><pre>搞gaodaima代码
ount($data);
for($i=0;$i<$num;$i++){
print_r($data);
}
}
?>
<?<BR>$handle=fopen("1.csv","r");<BR>$row=1;<BR>while($data=fgetcsv($handle,1000,",")){<BR>$num=count($data);<BR>for($i=0;$i<$num;$i++){<BR>echo $data[$i];<BR>}<BR>$row++;<BR>}<BR>?><BR>