所以我有一个表单和成功上传 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' 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);
}
}
}
}