✏️ 正在编辑: File.php
路径:
/srv/systems_dir/yuppiecred-lidernegocios/library/Core/File.php
提示:
您可以编辑任何文件(包括二进制文件),但请注意不当修改可能导致文件损坏。
<?php class Core_File { private $_path = "/downloads/"; private $_fileName = null; private $_startFileName; private $_hostFtp = null; private $_loginFtp = null; private $_passFtp = null; private $_typeFile = ".TXT"; private $_prefix = "NET"; public function getLoginFtp() { return $this->_loginFtp; } public function setLoginFtp($loginFtp) { $this->_loginFtp = $loginFtp; } public function getHostFtp() { return $this->_hostFtp; } public function setHostFtp($hostFtp) { $this->_hostFtp = $hostFtp; } public function getPassFtp() { return $this->_passFtp; } public function setPassFtp($passFtp) { $this->_passFtp = $passFtp; } public function setPath($bancoId) { $this->_path = "/downloads/" . $bancoId . "/"; } protected function findRemoteFile() { set_time_limit(300); // if( empty($this->_hostFtp) || empty($this->_loginFtp) || empty($this->_passFtp) ) // return null; $lastFileName = $this->findLocalFile(); $lastSequencial = $this->getSequencial($lastFileName); $sequencial = $this->formatSequencial($lastSequencial + 1); $fileName = $this->_startFileName . "_" . $sequencial . $this->_typeFile; // $ftp = new Core_Ftp($this->getHostFtp(), $this->getLoginFtp(), $this->getPassFtp()); // // if( $ftp->getConnection() != null ){ // try { // //entra no diretório que contém os arquivos // $ftp->changeDirectory(utf8_decode('Saída')); // //faz o download do arquivo para o diretório correspondente // $ftp->getFile($fileName, getcwd() . $this->_path . $fileName); // //fecha a conexão FTP // } catch ( Exception $e ) { // die("O arquivo não pode ser baixado. " . $e->getMessage()); // } // // $ftp->closeConnection(); // // if( file_exists(getcwd() . $this->_path . $fileName) ){ // $this->_fileName = $fileName; // } // } if( file_exists(getcwd() . $this->_path . $fileName) ){ $this->_fileName = $fileName; } } /** * Retorna o último arquivo baixado * @return type */ protected function findLocalFile() { $rs = opendir(getcwd() . $this->_path . "processed"); $files = array(); while ( $file = readdir($rs) ) { if( !is_dir($file) && (substr($file, 0, 3) == $this->_prefix ) ) array_push($files, $file); } closedir($rs); rsort($files); return isset($files[0]) ? $files[0] : null; } public function listFiles() { $folderPath = getcwd() . $this->_path; $rs = opendir($folderPath); $files = array(); $arquivo = new Model_DbTable_ArquivoImportado(); $filesImporteds = $arquivo->getFilesImporteds(); while ( $file = readdir($rs) ) { $pathInfo = pathinfo($folderPath . $file); if( is_file($folderPath . $file) && (filesize($folderPath . $file) > 82) && !in_array($file, $filesImporteds) && !empty($pathInfo['extension']) && (strtolower($pathInfo['extension']) == 'txt' || strtolower($pathInfo['extension']) == 'gz') ){ array_push($files, $file); } } closedir($rs); sort($files); return $files; } /** * Retorna o último sequencial * @param string $fileName * @return int */ protected function getSequencial($fileName) { if( $fileName == null ) return 0; $fileInfo = pathinfo(getcwd() . $this->_path . $fileName); $partsFileName = explode("_", $fileInfo['filename']); $startFileName = $partsFileName[0] . "_" . $partsFileName[1] . "_" . $partsFileName[2]; return ($startFileName == $this->_startFileName) ? (int) $partsFileName[3] : 0; } /** * Formata o sequencial para a exibição do arquivo * @param int $sequencial * @return string */ protected function formatSequencial($sequencial) { while ( strlen($sequencial) < 3 ) { $sequencial = "0" . $sequencial; } return $sequencial; } /** * Retorna os dados do arquivo $_fileName * @return array */ public function _readFile($fileName = '', $newPath = null) { $bancoId = $newPath; if( $newPath ){ $this->_path = $this->_path . "" . $newPath . "/"; } if( $fileName != '' ){ $filePath = getcwd() . $this->_path . $fileName; } else { $filePath = getcwd() . $this->_path . $this->_fileName; } if( !file_exists($filePath) ) return array(); $pathInfo = pathinfo($filePath); if( strtolower($pathInfo['extension']) == 'txt' ){ $dataRead = file($filePath); } if( strtolower($pathInfo['extension']) == 'gz' ){ $dataRead = $this->_unGzFileXml($filePath); } $this->_gravarArquivoImportado($fileName, $bancoId); return $dataRead; } protected function _unGzFileXml($file) { $pathInfo = pathinfo($file); $newFile = $pathInfo['dirname'] . '/' . $pathInfo['filename'] . ".xml"; if( is_file($file) && strtolower($pathInfo['extension']) == 'gz' ){ if( !is_file($newFile) ){ $filter = new Zend_Filter_Decompress(array( 'adapter' => 'Gz', 'options' => array( 'target' => $pathInfo['dirname'], 'mode' => 'deflate', ) )); $desc = $filter->filter($file); file_put_contents($newFile, $desc); } $xml = simplexml_load_file($newFile); $arrayObj = array(); foreach ( $xml as $value ) { if( is_object($value) ){ $arrayObj[] = $value; } else { foreach ( $value as $obj ) { $arrayObj[] = $obj; } } } //remover arquivo compactado // unlink($file); return $this->obj2array($arrayObj, null, 'int'); } return null; } public function obj2array($xmlObject, $out = NULL, $typeIndex = 'varchar') { foreach ( (array) $xmlObject as $index => $node ) { $out2 = NULL; if( ( is_object($node) ) ){ $out2 = $this->obj2array($node); } else { $out2 = $node; } if( $typeIndex == 'int' ){ $out[] = $out2; } else { $out[$index] = $out2; } } return $out; } protected function _gravarArquivoImportado($fileName, $bancoId) { $arquivo = new Model_DbTable_ArquivoImportado(); $arquivo->insert(array( 'arquivo' => $fileName, 'banco_id' => $bancoId )); if( file_exists($this->_path . $fileName) ){ unlink(getcwd() . $this->_path . $fileName); } } /** * Prepara os dados do arquivo para fazer a importação * @return array */ public function prepareNet($line) { $dataPrepared = null; if( count($line) > 0 ){ $data = explode('#', $line); //verifica se o código da promotora está setado para preenher os dados if( isset($data[1]) && $data[1] != null ){ $dataPrepared['codigo_promotora'] = isset($data[1]) ? $data[1] : null; $dataPrepared['nome_promotora'] = isset($data[2]) ? $data[2] : null; $dataPrepared['codigo_convenio'] = isset($data[3]) ? $data[3] : null; $dataPrepared['convenio'] = isset($data[4]) ? $data[4] : null; $dataPrepared['cliente'] = isset($data[6]) ? $data[6] : null; $dataPrepared['matricula'] = isset($data[7]) ? $data[7] : null; $dataPrepared['cpf'] = isset($data[8]) ? $data[8] : null; $dataPrepared['data_nascimento'] = isset($data[9]) ? $data[9] : null; $dataPrepared['tipo_documento'] = isset($data[10]) ? $data[10] : null; $dataPrepared['numero_documento'] = isset($data[11]) ? $data[11] : null; $dataPrepared['orgao_emissor'] = isset($data[12]) ? $data[12] : null; $dataPrepared['uf_emissor'] = isset($data[13]) ? $data[13] : null; $dataPrepared['data_expedicao'] = isset($data[14]) ? $data[14] : null; $dataPrepared['endereco'] = isset($data[15]) ? $data[15] : null; $dataPrepared['complemento'] = isset($data[16]) ? $data[16] : null; $dataPrepared['bairro'] = isset($data[17]) ? $data[17] : null; $dataPrepared['cidade'] = isset($data[18]) ? $data[18] : null; $dataPrepared['uf'] = isset($data[19]) ? $data[19] : null; $dataPrepared['cep'] = isset($data[20]) ? $data[20] : null; $dataPrepared['telefone'] = isset($data[21]) ? $data[21] : null; $dataPrepared['referencia'] = isset($data[22]) ? $data[22] : null; $dataPrepared['telefone_referencia'] = isset($data[23]) ? $data[23] : null; $dataPrepared['codigo_produto'] = isset($data[24]) ? $data[24] : null; $dataPrepared['nome_produto'] = isset($data[25]) ? $data[25] : null; $dataPrepared['tipo_produto'] = isset($data[26]) ? $data[26] : null; $dataPrepared['numero_proposta'] = isset($data[27]) ? $data[27] : null; $dataPrepared['status_proposta'] = isset($data[28]) ? $data[28] : null; $dataPrepared['parcela'] = isset($data[29]) ? $data[29] : null; $dataPrepared['valor_parcela'] = !empty($data[30]) ? number_format($data[30], 2, ',', '.') : null; $dataPrepared['forma_liberacao'] = isset($data[31]) ? $data[31] : null; $dataPrepared['banco_credito'] = isset($data[32]) ? $data[32] : null; $dataPrepared['agencia'] = isset($data[33]) ? $data[33] : null; $dataPrepared['conta'] = isset($data[34]) ? $data[34] : null; $dataPrepared['banco_saque'] = isset($data[35]) ? $data[35] : null; $dataPrepared['agencia_saque'] = isset($data[36]) ? $data[36] : null; $dataPrepared['numero_associado'] = isset($data[37]) ? $data[37] : null; $dataPrepared['valor_solicitado'] = !empty($data[38]) ? number_format($data[38], 2, ',', '.') : null; $dataPrepared['percentual_comissao'] = !empty($data[39]) ? number_format($data[39], 2, ',', '.') : null; $dataPrepared['valor_bruto_comissao'] = !empty($data[40]) ? number_format($data[40], 2, ',', '.') : null; $dataPrepared['situacao_comissao'] = isset($data[41]) ? $data[41] : null; // > 0 = paga $dataPrepared['data_pagamento_comissao'] = isset($data[42]) ? $data[42] : null; $dataPrepared['valor_quitacao'] = !empty($data[43]) ? number_format($data[43], 2, ',', '.') : null; $dataPrepared['nome_consignataria'] = !empty($data[44]) ? $data[44] : null; $dataPrepared['data_inclusao_proposta'] = isset($data[45]) ? $data[45] : null; $dataPrepared['saldo_devedor'] = isset($data[46]) ? $data[46] : null; $dataPrepared['valor_liberado'] = !empty($data[47]) ? number_format($data[47], 2, ',', '.') : null; $dataPrepared['codigo_banco_destinatario_credito'] = isset($data[48]) ? $data[48] : null; $dataPrepared['codigo_banco_saque_credito'] = isset($data[49]) ? $data[49] : null; $dataPrepared['codigo_vendedor'] = isset($data[50]) ? $data[50] : null; $dataPrepared['codigo_usuario'] = isset($data[51]) ? $data[51] : null; $dataPrepared['nome_usuario'] = isset($data[52]) ? $data[52] : null; $dataPrepared['sexo'] = isset($data[53]) ? $data[53] : null; $dataPrepared['estado_civil'] = isset($data[54]) ? $data[54] : null; $dataPrepared['nome_conjuge'] = isset($data[55]) ? $data[55] : null; $dataPrepared['mae'] = isset($data[56]) ? $data[56] : null; $dataPrepared['pai'] = isset($data[57]) ? $data[57] : null; $dataPrepared['numero_contrato'] = isset($data[58]) ? $data[58] : null; $dataPrepared['data_contrato'] = isset($data[59]) ? $data[59] : null; $dataPrepared['tabela_financiamento'] = isset($data[60]) ? $data[60] : null; $dataPrepared['taxa_financiamento'] = !empty($data[61]) ? number_format($data[61], 2, ',', '.') : null; $dataPrepared['valor_tac'] = !empty($data[62]) ? number_format($data[62], 2, ',', '.') : null; $dataPrepared['valor_beneficio'] = !empty($data[63]) ? number_format($data[63], 2, ',', '.') : null; $dataPrepared['valor_financiado'] = !empty($data[64]) ? number_format($data[64], 2, ',', '.') : null; $dataPrepared['data_1_vencimento'] = isset($data[65]) ? $data[65] : null; $dataPrepared['codigo_filial'] = isset($data[66]) ? $data[66] : null; $dataPrepared['codigo_empresa'] = isset($data[67]) ? $data[67] : null; $dataPrepared['obs'] = isset($data[68]) ? $data[68] : null; $dataPrepared['status_retorno'] = isset($data[69]) ? $data[69] : null; $dataPrepared['data_atualizacao'] = isset($data[70]) ? $data[70] : null; $dataPrepared['anterior_numero'] = isset($data[71]) ? $data[71] : null; $dataPrepared['anterior_prazo'] = isset($data[72]) ? $data[72] : null; $dataPrepared['anterior_parcelas_pagas'] = isset($data[73]) ? $data[73] : null; $dataPrepared['anterior_sequencial_compra'] = isset($data[74]) ? $data[74] : null; $dataPrepared['anterior_tipo_quitacao'] = isset($data[75]) ? $data[75] : null; } //copia o arquivo para a pasta de arquivos processados e o exclui // copy(getcwd() . $this->_path . $this->_fileName, getcwd() . $this->_path . "processed/" . $this->_fileName); // unlink(getcwd() . $this->_path . $this->_fileName); } return $dataPrepared; } protected function loadDataToDatabase($fileName) { $db = new Zend_Db_Table(); $profiler = $db->getAdapter()->getProfiler(); $profiler->setEnabled(true); $query = "CREATE TABLE IF NOT EXISTS tab_temp ("; $query .= " linhas text"; $query .= " )engine=myisam;"; $db->getAdapter()->query($query); //Para conexao com outro servidor deve-se passar o parametro LOCAL $queryCreate = "SET AUTOCOMMIT=0; TRUNCATE tab_temp; LOAD DATA LOCAL INFILE '" . getcwd() . $this->_path . $fileName . "'" // $queryCreate = "SET AUTOCOMMIT=0; TRUNCATE tab_temp; LOAD DATA INFILE '" . getcwd() . $this->_path . $fileName . "'" . " INTO TABLE tab_temp" // . " CHARACTER SET utf8" // . " FIELDS TERMINATED BY '#'" . " LINES TERMINATED BY '\n';" . " COMMIT; SET AUTOCOMMIT=1;"; $db->getAdapter()->query($queryCreate); return $db->getAdapter()->fetchAll("SELECT * FROM tab_temp"); } public function loadCsvToDatabase($fileName) { $db = new Zend_Db_Table(); $profiler = $db->getAdapter()->getProfiler(); $profiler->setEnabled(true); $query = "CREATE TABLE IF NOT EXISTS tab_temp2 ("; $query .= " linhas text"; $query .= " )engine=myisam;"; $db->getAdapter()->query($query); //Para conexao com outro servidor deve-se passar o parametro LOCAL $queryCreate = "SET AUTOCOMMIT=0; TRUNCATE tab_temp2; LOAD DATA LOCAL INFILE '" . $fileName . "'" // $queryCreate = "SET AUTOCOMMIT=0; TRUNCATE tab_temp2; LOAD DATA INFILE '" . $fileName . "'" . " INTO TABLE tab_temp2" // . " CHARACTER SET utf8" // . " FIELDS TERMINATED BY ';'" // . " ESCAPED BY '\\'" . " LINES TERMINATED BY '\r'" . " IGNORE 1 LINES" . ";" . " COMMIT; SET AUTOCOMMIT=1;"; $db->getAdapter()->query($queryCreate); // $this->lockTmpTable(); unlink($fileName); return $db->getAdapter()->fetchAll("SELECT * FROM tab_temp2"); } public function lockTmpTable() { $db = new Zend_Db_Table(); return $db->getAdapter()->query("LOCK TABLE tab_temp2 READ;"); } public function unLockTmpTable() { // $db = new Zend_Db_Table(); // return $db->getAdapter()->query("UNLOCK TABLES"); } public function listFilesGz() { $folderPath = getcwd() . $this->_path; $rs = opendir($folderPath); $files = array(); $newFile = null; $arquivo = new Model_DbTable_ArquivoImportado(); $filesImporteds = $arquivo->getFilesImporteds(); while ( $file = readdir($rs) ) { $pathInfo = pathinfo($folderPath . $file); if( is_file($folderPath . $file) && !in_array($file, $filesImporteds) && strtolower($pathInfo['extension']) == 'gz' ){ //descompacta o arquivo gz, para xml $filter = new Zend_Filter_Decompress(array( 'adapter' => 'Gz', 'options' => array( 'target' => $folderPath, 'mode' => 'deflate', 'archive' => $file ) )); //filtra o caminho $desc = $filter->filter($folderPath . $file); $newFile = substr($file, 0, strpos($file, '.')) . '.xml'; file_put_contents($folderPath . $newFile, $desc); unlink($folderPath . $file); array_push($files, $newFile); $xml = simplexml_load_file($folderPath . $newFile); $this->_lerXmlBonSucesso($xml); } } closedir($rs); sort($files); return $files; } public function _lerXmlBonSucesso($xml) { $dados = array(); foreach ( $xml->Table as $reader ) { $dados['produto'] = current($reader->COD_PRODUTO); $dados['descricao'] = current($reader->DSC_PRODUTO); $dados['data_inclusao'] = current($reader->DAT_CTR_INCLUSAO); $dados['data_alteracao'] = current($reader->DAT_CTR_ALTERACAO); $reader->NOM_CTR_PROCESSO; $reader->NOM_CTR_ACESSO; $reader->ATIVO; $saveXml = $reader; } } }
💾 保存文件
← 返回文件管理器