<form enctype="multipart/form-data"action="import.php" method="POST">
<labelfor="文件选择">文件选择:</label><inputname="csv_goods" type="file" />
<input type="submit" value="导入" name="import"/>
</form>
<?php
if (isset($_POST['import'])){
$file =$_FILES['csv_goods'];
$file_type =substr(strstr($file['name'],'.'),1);
//检查文件格式
if($file_type != 'csv'){
echo '文件格式不对,请重新上传!';
exit;
}
$handle =fopen($file['tmp_name'],"r");
$file_encoding =mb_detect_encoding($handle);
//检查文件编码
if($file_encoding != 'ASCII'){
echo '文件编码错误,请重新上传!';
exit;
}
$row =0;
while ($data= fgetcsv($handle,1000,',')){
//echo "<fontcolor=red>$row</font>"; //可以知道总共有多少行
$row++;
if ($row == 1)
continue;
$num = count($data);
// 这里会依次输出每行当中每个单元格的数据
for ($i=0; $i<$num; $i++){
echo$data[$i]."<br>";
// 在这里对数据进行处理
}
}
fclose($handle);
}
?>