读取两文件,不同的内容存入另一个文件中

来源:互联网 发布:fedora linux镜像下载 编辑:程序博客网 时间:2024/06/07 20:15
  1. header("Content-type:text/html;charset=utf-8");  
  2. class Readfiledata {  
  3.     /** 
  4.      * 链接数据库 
  5.      */  
  6.     private static  function connect(){  
  7.         require_once 'index2.php';  
  8.         mysql_connect('localhost','root','');  
  9.         mysql_select_db('sinapay');  
  10.     }  
  11.     /** 
  12.      * 读文件并获取数据 
  13.      */  
  14.     private static function getdata($file) {  
  15.         $handle = fopen ( $file'r' );  
  16.         $orderform = array ();  
  17.         $i=0;  
  18.         while ( false != ($data = fgetcsv ($handle,0,',')) ) {  
  19.             $i++;  
  20.             if($i==0) continue;  
  21.             $orderform [] =trim($data [0],"\t\n\r\0\x0B`\'");  
  22.         }  
  23.         fclose ( $handle );  
  24.         return $orderform;  
  25.     }  
  26.     /** 
  27.      * 获取两个文件不同的数据 
  28.      * 
  29.      * @param String $file1          
  30.      * @param String $file2          
  31.      */  
  32.     private static function getdiffdata($file1$file2) {  
  33.         $orderform = self::getdata ( $file1 );  
  34.         $orderform2 = self::getdata ( $file2 );  
  35.         $diff1 = array_diff ($orderform,$orderform2 );  
  36.         $diff2 = array_diff ($orderform2,$orderform );  
  37.         $todiff = array_merge ( $diff1$diff2 );  
  38.         $todif=array_values(array_unique($todiff));  
  39.         return $todif;  
  40.     }  
  41.     /** 
  42.      * 数据写入.csv 文件中 
  43.      * @param String $filename           
  44.      * @param String $file1          
  45.      * @param String $file2          
  46.      */  
  47.     private static function writefile($filename$file1$file2) {  
  48.         $todiffdata = self::getdiffdata ( $file1$file2 );  
  49.         $newarray=array();  
  50.         self::connect();  
  51.         if(empty($todiffdata)){  
  52.             echo "两个文件的数据一致";  
  53.             die;  
  54.         }  
  55.         $toparray=array("charge_id","bussined_id");  
  56.         $condition='';  
  57.         $counarray=count($todiffdata)-1;  
  58.         foreach($todiffdata as $key=>$val){  
  59.           if($key==$counarray){  
  60.             $condition.="charge_id=$val";  
  61.           }else{  
  62.             $condition.="charge_id=$val or ";  
  63.           }  
  64.         }  
  65.         $sql="select charge_id,business_id from sinapay_charge_final where $condition";  
  66.         $result=mysql_query($sql);  
  67.         while($res=mysql_fetch_array($result)){  
  68.             $data=array('charge_id'=>$res['charge_id'],'business_id'=>$res['business_id']);  
  69.             $newarray[]=$data;  
  70.         }  
  71.         if (! is_file ( $filename)) {  
  72.              touch ( $filename);  
  73.         }  
  74.         $handle = fopen ($filename'a' );  
  75.         fputcsv($handle$toparray);  
  76.         foreach($newarray as $value){  
  77.           fputcsv ( $handle$value );  
  78.         }  
  79.         fclose ( $handle );  
  80.     }  
  81.       
  82.     /** 
  83.      * 入口文件 
  84.      */  
  85.     public static function main($filename,$file1,$file2) {  
  86.         self::writefile ( $filename$file1$file2 );  
  87.     }  
  88. }  
  89.   
  90. $filename = 'total.csv';  
  91. $file1 = 'ac.csv';  
  92. $file2 = 'ad.csv';  
  93. Readfiledata::main ($filename$file1$file2 ); 
阅读全文
0 0
原创粉丝点击