Php 解析器仅获取csv列标题问题
所以我有一个表单和解析器,它成功地上传了一个csv文件并对其进行解析,但是我只想获取csv文件中的列标题。我如何只获取第一行的所有列标题Php 解析器仅获取csv列标题问题,php,Php,所以我有一个表单和解析器,它成功地上传了一个csv文件并对其进行解析,但是我只想获取csv文件中的列标题。我如何只获取第一行的所有列标题 function ShowForm() { print"<form method='post' action='$_SERVER[PHP_SELF]' enctype='multipart/form-data'>\n"; print"<p>enter csv file:\n". "<input type='file' n
function ShowForm()
{
print"<form method='post' action='$_SERVER[PHP_SELF]' enctype='multipart/form-data'>\n";
print"<p>enter csv file:\n".
"<input type='file' name ='csv' value=''/>\n"."</p>".
"<p><input type='submit' name ='submit' />".
"</p>".
"</form>";
}
//////////////////////////////////////////////////////////////////////////////////
function Processform()
{
$csv = array();
// check there are no errors
if($_FILES['csv']['error'] == 0){
$name = $_FILES['csv']['name'];
$ext = strtolower(end(explode('.', $_FILES['csv']['name'])));
$type = $_FILES['csv']['type'];
$tmpName = $_FILES['csv']['tmp_name'];
print "name : $name<br >";
print "extenstion : $ext<br >";
print "type : $type<br >";
print "name : $tmpName<br >";
if($ext === 'csv'){
if(($handle = fopen($tmpName, 'r')) !== FALSE) {
// necessary if a large csv file
set_time_limit(0);
$row = 0;
while(($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
// number of fields in the csv
$num = count($data);
// get the values from the csv
$csv[$row]['row1'] = $data[0];
$csv[$row]['row2'] = $data[1];
// inc the row
$row++;
}
fclose($handle);
}
}
}
}
函数ShowForm()
{
打印“\n”;
打印“输入csv文件:\n”。
“\n”。”。
“”。
“”。
"";
}
//////////////////////////////////////////////////////////////////////////////////
函数Processform()
{
$csv=array();
//检查是否没有错误
如果($\u文件['csv']['error']==0){
$name=$_文件['csv']['name'];
$ext=strtolower(结束(分解('.',$_文件['csv']['name']));
$type=$_文件['csv']['type'];
$tmpName=$\u文件['csv']['tmp\u名称'];
打印“名称:$name
”;
打印“扩展名:$ext
”;
打印“类型:$type
”;
打印“名称:$tmpName
”;
如果($ext=='csv'){
if($handle=fopen($tmpName,'r'))!==FALSE){
//如果是大型csv文件,则需要
设置时间限制(0);
$row=0;
while(($data=fgetcsv($handle,1000,,'))!==FALSE){
//csv中的字段数
$num=计数($data);
//从csv获取值
$csv[$row]['row1']=$data[0];
$csv[$row]['row2']=$data[1];
//包括争吵
$row++;
}
fclose($handle);
}
}
}
}
fgetcsv方法从CSV文件中获取一行,因此要获取第一行,只需调用它一次:
if(($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
// handle first row
}
$row = 1;
// Now handle all the rest
while(($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
// number of fields in the csv
$num = count($data);
// get the values from the csv
$csv[$row]['row1'] = $data[0];
$csv[$row]['row2'] = $data[1];
// inc the row
$row++;
}