<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>信息发布</title>
<link rel="stylesheet" href="../css/css.css" type="text/css">

 
<?php 
	   include("../conn.php"); 
	   $classname=$_POST['changci'];
	 
?>


<?php 


	$sql="select * from  classtable ";
	$sql=$sql."  where classname='".$classname."'";
	$rs=$conn->execute($sql);

	if (!$rs->EOF)
	{
		$get_classname_id_value=$rs->fields["id"];
	}
	$rs->close;
 
?>




<?php 

	        
           if(empty($_FILES['file']['name']))
	       {  
				 //$_FILES['file_name']['name']为获取客户端机器文件的原名称
		       
				 
				 $uurl="admin_stu_import.php?classname=".urlencode($get_changci);
	   			 $uurl=$uurl."&classname_id_value=".$get_classname_id_value;
	   			 echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
	   			 echo "<body>";
	   			 echo "<script language=javascript>";
	   			 echo "alert('[软件提示]:请选择你要上传的EXCEL学生信息文件,请将文件另存为CSV 格式.');";
       			 echo "window.location='$uurl';";
	   			 echo "</script>";	
       			 echo "</body>";
	   			 exit;
				 
				 
				 
				 
           }
		   
		   
		   if(!empty($_FILES['file']['name']))
		   {
		   
               
			    $oldfilename=$_FILES['file']['name'];
		   
		        //echo  "<br>";
		        //echo  "----上载信息如下------：";
		        //echo  "<br>";
                //echo "<br>A.原文件名为：".$oldfilename."<br>";    
	   
	            $start=strrpos($oldfilename,".");
                $filetype=substr($oldfilename,$start,strlen($oldfilename)-strrpos($oldfilename,"."));
                //echo "<br>B.原文件的类型为：".$filetype."<br>";
		        $filetype=strtoupper($filetype);
		   
               if( $filetype!='.CSV')  
			   {
                 
				 // echo "B.文件类型是:".$filetype."<br>";
				 
				 $uurl="admin_stu_import.php?classname=".urlencode($get_changci);
	   			 $uurl=$uurl."&classname_id_value=".$get_classname_id_value;
	   			 echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
	   			 echo "<body>";
	   			 echo "<script language=javascript>";
	   			 echo "alert('[软件提示]：不能上传Excel非CSV格式的文件.');";
       			 echo "window.location='$uurl';";
	   			 echo "</script>";	
       			 echo "</body>";
	   			 exit;
				 
				 
				 
				 
				 
                }
		  
                //echo "<br>C.上传文件的大小为（字节）：".$_FILES['file']['size'];
              
			   $max_size=30*1024*1024;   ///--------------------------20M
			 
			   if ($_FILES['file_name']['size']>$max_size) 
			   {
                				 
				 $uurl="admin_stu_import.php?classname=".urlencode($get_changci);
	   			 $uurl=$uurl."&classname_id_value=".$get_classname_id_value;
	   			 echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
	   			 echo "<body>";
	   			 echo "<script language=javascript>";
	   			 echo "alert('[软件提示]:文件大小超过30M，请改写后上传.');";
       			 echo "window.location='$uurl';";
	   			 echo "</script>";	
       			 echo "</body>";
	   			 exit;
				
				 
               }
		   
		       //取得保存文件的临时文件名（含路径）
		       //echo "<br>文件上传时的临时文件名为：".$_FILES['file']['tmp_name'];
					
			   //$new_filename=date("Y_m_d")."_".$oldfilename; 
			   srand((double)microtime()*1000000);
               $rand1=rand(1,9);
			   $rand2=rand(1,9);
               $rand=$rand1.$rand2;
			  
			
			   $new_filename="student".$filetype;
			 		
           
		       //echo "<br>A.上传后文件名为：".$new_filename;  
			   //echo  "<br>"; 
			   //echo "<br>B.上传文件的大小为（字节）：".$_FILES['file']['size']."Bytes";
			   //echo  "<br>"; 
		
		       $new_filename=strtolower($new_filename);
			
		       $db_filename=strtolower($new_filename);   //utf-8 存入数据库，没有乱码
			
			   //---先转换为GB2312，上传成功！汉字没有乱码----------------------------
			   $new_filename=iconv("UTF-8","gb2312", $new_filename);  //
			
               $savedir="../student_info/".$new_filename;
	   
               if(move_uploaded_file($_FILES['file']['tmp_name'],$savedir))
	           {
                   $file_name=basename($savedir);       //取得保存文件的文件名（不含路径）
                   //echo "<br>文件上传成功！保存为：".$savedir;   //显示有乱码
				   echo "<br>文件上传成功！文件名保存在：<font color=#0000ff>./student_info/".$db_filename."</font>";
				   echo "<br>";
               }
	           else
	           {
                  
				  $uurl="admin_stu_import.php?classname=".urlencode($get_changci);
	   			  $uurl=$uurl."&classname_id_value=".$get_classname_id_value;
	   			  echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
	   			  echo "<body>";
	   			  echo "<script language=javascript>";
	   			  echo "alert('[软件提示]:错误!无法将文件上传，请检查站点根目录下是否存在 student_info 目录，以及拥有的权限.');";
       			  echo "window.location='$uurl';";
	   			  echo "</script>";	
       			  echo "</body>";
	   			  exit;
				  			  
				  
               }
		        
			
		
		
		   }
		
				 
	?>
	
	
	<?php 
	
	       $get_changci=$_POST["changci"];
	
	 ?>
    
    
  
    <? 

      
	  $temp=file("../student_info/student.csv");//连接EXCEL文件,格式为了.csv 
	  //----------要检查考生ID 要在数据库中不存在，可行-------------------------
	  $exist_flag=0;
	  
	  for ($i=0;$i<count($temp);$i++) 
      { 
		$string=explode(",",$temp[$i]);//通过循环得到EXCEL文件中每行记录的值 
		$str_id=iconv("gb2312","UTF-8",$string[0]);
		$exist_flag=0;
		$sql="select * from  studentinfo where  studentid='".$str_id."'";
		$rs=$conn->execute($sql);
		if (!$rs->EOF)
		{
			$exist_flag=1;
			break;
		}
		$rs->close;
		
      }
	  
	  if  ($exist_flag==1)
	  {
	 
	   $uurl="admin_stu_import.php?classname=".urlencode($get_changci);
	   $uurl=$uurl."&classname_id_value=".$get_classname_id_value;
	   echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
	   echo "<body>";
	   echo "<script language=javascript>";
	   echo "alert('【软件提示】:所填写考生准考证号已经在数据库中重复.');";
       echo "window.location='$uurl';";
	   echo "</script>";	
       echo "</body>";
	   exit;
	   
	  }
	  
	  
       
	   
       for ($i=0;$i<count($temp);$i++) 
      { 
        
		
		
		$string=explode(",",$temp[$i]);//通过循环得到EXCEL文件中每行记录的值 
		
		$str_id=iconv("gb2312","UTF-8",$string[0]);
		$stu_str=iconv("gb2312","UTF-8",$string[1]);
		$stu_str=trim($stu_str);
				
	    $testclassname_str=$get_changci;
       
        $getpassword="";
		
        while(strlen($getpassword)<5)
        {

          mt_srand((double)microtime()*1000000);
          $getnum=rand(1,9);
          if ($getnum!=0)
          {
            $getpassword=$getpassword.$getnum;
          } 
        } 

		
        $sql="insert into studentinfo(testclassname,studentid,name,password) values(";
	    $sql=$sql."'".$testclassname_str."',";
		$sql=$sql."'".$str_id."',";
		$sql=$sql."'".$stu_str."',";
		$sql=$sql."'".$getpassword."'";
	    $sql=$sql." )";
        
		//echo $sql."<br>";
		$re=mysql_query($sql);
		unset($string); 

	  }
       
	  if (!mysql_error()); 
      { 
          //echo " 成功导入数据!; 
		   $uurl="admin_stu_import.php?classname=".urlencode($get_changci);
		   $uurl=$uurl."&classname_id_value=".$get_classname_id_value;
		   echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
	       echo "<body>";
	       echo "<script language=javascript>";
		   echo "alert('【软件提示】:考生信息已经成功导入,请查看.');";
		   echo "window.location='$uurl';";
	       echo "</script>";	
           echo "</body>";
		 
      } 
	  
      
   
   
   
   
   ?>
   

   
   
   
   
   
   
   

</body>
</html>
