导入CVS数据

<form ENCTYPE="multipart/form-data" ACTION="" METHOD="POST">  
      <p>导入CVS数据    <input NAME="MyFile" TYPE="file"> <input VALUE="提交" TYPE="submit"></p> 
</form>  
<?php 
if(isset($_FILES['MyFile']['name'])){ 
$file_type = substr(strstr($_FILES['MyFile']['name'],'.'),1);  
// 检查文件格式 
if ($file_type != 'csv'){ 
echo '文件格式不对,请重新上传!'; 
exit; 
} 
error_reporting(0); 
//$connect=mysql_connect("localhost","root","pass8899") or die("could not connect to database"); 
//mysql_select_db("lf_magentotest",$connect) or die (mysql_error()); 
$fname = $_FILES['MyFile']['tmp_name']; 
$handle=fopen("$fname","r"); 
  
$line_title = fgetcsv($handle,10000,",");//title 
while($data=fgetcsv($handle,10000,","))            
{ 
      $q="insert into test (code,name,date) values ('$data[0]','$data[1]','$data[2]')"; 
 echo "$q<br>"; 
     // mysql_query($q) or die (mysql_error()); 
                                      
} 
fclose($handle); 
echo"导入数据成功<br>";  
} 
?> 

你可能感兴趣的:(导入CVS数据)