// 導出csv public function exportCSV($date = '2017-12-01',$prefix='a',$fileDir='b'){ $CsvFile = $fileDir.$prefix.$this->format($date).'.csv'; //上傳後的文件名稱 $fp = fopen($CsvFile, 'w'); // 表頭標題 $header_data = ['Trading Platform ID(Login)','Client-First Name','Client-surname','Client-date of birth','Client-Country','National Identifier']; foreach ($header_data as $key => $value) { $header_data[$key] = iconv('utf-8', 'gbk', $value); //轉碼 } fputcsv($fp, $header_data); // 數據 $startDate = $date . ' 00:00:00'; $endDate = $date . ' 23:59:59'; $sql = "select Cln_ID,First_Name,Surname,Birth_Date,Country from cln_apply where (Input_Date>'$startDate' AND Input_Date<'$endDate')"; $clnmodel = new ClnModel(); $result = $clnmodel->findAll($sql); if($result){ foreach ($result as $val) { $arr = (array)$val; foreach($arr as $k=>$v){ if($k == 'Birth_Date'){ $v = substr($v,0,10); } $arr[$k] = iconv('utf-8', 'gbk', $v); //轉碼 } fputcsv($fp, $arr); } } } // 時間格式化 public function format($date){ return join(explode('-',$date)); } // 上傳csv文件到Ftp // 參數 $date 格式 YYYY-MM-DD public function uploadCSV($date = '2017-12-01'){ $host = ''; //FTP主機 $user = ''; //ftp用戶名 $pass = ''; //ftp密碼 $prefix = 'ATXUK_client_'; //文件名前綴 $fileDir = __DIR__.'/../mifi_report/'; //Mifi_Report文件的目錄 // 生成CSV文件 $CSV = $this->exportCSV($date,$prefix,$fileDir); // 鏈接登陸ftp $conn = ftp_connect($host) or die("Could not connect"); ftp_login($conn, $user, $pass); $remote = $prefix.$this->format($date).'.csv'; //上傳後的文件名稱 $local = $fileDir.$remote; //本地要上傳的文件 $csvresult = ftp_put($conn, $remote, $local, FTP_BINARY); //上傳第一個文件.csv // var_dump($csvresult); if($csvresult){ $nullFile = $prefix.$this->format($date).'.done'; //上傳成功備註 $nullresult = ftp_put($conn, $nullFile, $fileDir.'mifi.done', FTP_ASCII); //上傳第二個空文件.done // var_dump($nullresult); } ftp_close($conn); }