下面来看一个商品导入csv文件的方法:
setlocale(LC_ALL, 'zh_CN');
$file = $_FILES['files'];
$file_type = substr(strstr($file['name'],'.'),1);
if ($file_type != 'csv'){
$this->show_message("".'请选择正确的文件上传!'."", '1', $links);
exit;
}
$handle = fopen($file['tmp_name'],"r");
$file_encoding = mb_detect_encoding($handle);
if ($file_encoding != 'ASCII'){
$this->show_message("".'文件编码错误,请重新上传!'."", '1', $links);
exit;
}
$row = 0;
$str="";
$sy="";
while ($data = fgetcsv($handle,1000,',')){
$row++;
if ($row == 0)
continue;
$num = count($data);
for ($i=0; $i
$str = (string)$data[$i].'|';
$str = mb_convert_encoding($str, "UTF-8", "GBK");
$sy .= $str;
}
}