当每天使用任务计划程序运行文件时,如何按需将文件上传到数据库

时间:2018-10-03 08:04:07

标签: php

我有以下代码,该代码计划每天使用任务计划程序在午夜运行。首先,我的代码连接到数据库(函数__construct),然后将其日志文件夹中的文件与已经上传的文件(函数uploadLogs)进行比较,然后函数uploadLog将文件上传到未上传的数据库中。上载的文件被写入文件以复制数据(功能updateRegistery)。

现在,一次上传文件后,便无法再次上传,因为该名称已经写入注册表文件中。但是我想每天运行它。我真的想不出一种方法。有人可以帮我编码吗?

我的代码:

<?php

class logAgent
{
    const CONFIG_FILENAME = "data_config.ini";
    
    private $_dbConn;
    private $_config;
    
    
    function __construct()
    {
        $this->_loadConfig();
        
        
        $this->_dbConn = oci_connect($this->_config['db_usrnm'],
            $this->_config['db_pwd'],
            $this->_config['hostnm_sid']);
    }
    
   public function uploadLogs(){
        
        //Comparison of all the files present in the logs directory and all the files which has been already uploaded
        $f = fopen($this->_config['uploadedRegistry'], 'r');
        $contents = [];
        while (FALSE !== ($row = fgetcsv($f, 1000, $this->_config['filenameTimeSeparator']))){
            $contents[] = $row[0];
        }
		$files = array_map('basename',glob($this->_config['logspath'] . $this->_config['fileextension'],GLOB_BRACE));
        $result = array_diff($files, $contents);
        foreach($result as $r){
            $this->uploadLog($r);
        }
    }
    
    
    private function _loadConfig()
    {
        // Loads config
        $path = dirname(__FILE__) . '/' . self::CONFIG_FILENAME;
        $this->_config = parse_ini_file($path) ;
    }
    
    public function uploadLog($filename) {
        
        //Uploads file to database
        $filename = trim($this->_config['logspath'] . trim($filename));
		      
        if(file_exists($filename)){
            $myfile = fopen($filename, "r");
            while(!feof($myfile)) {
                $content= fgets($myfile);
                $carray=explode($this->_config['logFomatDelimiter'],$content);

                list($REQUEST_TIME, $HOSTNAME, $IP_ADDRESS, $WORKFLOW_NAME, $USERID, $EVENT_MESSAGE , $OPTIONAL_DETAILS , $SESSION_COOKIES)=$carray;
				
				$data = array_map(function($v) { return explode('=',trim($v));},explode(';',substr($carray[6],strpos($carray[6],'=')+1)));
				foreach ($data as $datum) {
					if ($datum[0] == 'TANID') {
						$SESSION_COOKIES= $datum[0] . "=" . $datum[1];
						break;
					}
				}
                $statement = "INSERT INTO AUTH_LOGS(REQUEST_TIME, HOSTNAME, IP_ADDRESS, WORKFLOW_NAME, USERID, EVENT_MESSAGE , OPTIONAL_DETAILS , SESSION_COOKIES) 
                 values(to_date(:REQUEST_TIME, 'YYYY/MM/DD HH24:MI:SS'), :HOSTNAME, :IP_ADDRESS, :WORKFLOW_NAME , :USERID ,:EVENT_MESSAGE ,
				 :OPTIONAL_DETAILS, :SESSION_COOKIES)";
                
                //Preparing an Oracle statement for execution
                $compiled = oci_parse($this->_dbConn, $statement);
                
                //binding values to named parameters
				oci_bind_by_name($compiled, ':REQUEST_TIME', $REQUEST_TIME);
                oci_bind_by_name($compiled, ':HOSTNAME', $HOSTNAME);
                oci_bind_by_name($compiled, ':IP_ADDRESS', $IP_ADDRESS);
                oci_bind_by_name($compiled,':WORKFLOW_NAME', $WORKFLOW_NAME);
                oci_bind_by_name($compiled, ':USERID', $USERID);
				$EVENT_MESSAGE = str_replace('"', '', $EVENT_MESSAGE);
                oci_bind_by_name($compiled, ':EVENT_MESSAGE', $EVENT_MESSAGE);
				$OPTIONAL_DETAILS = str_replace('"', '', $OPTIONAL_DETAILS);
                oci_bind_by_name($compiled, ':OPTIONAL_DETAILS', $OPTIONAL_DETAILS);
				$SESSION_COOKIES = str_replace(')"', '', $SESSION_COOKIES);
				oci_bind_by_name($compiled, ':SESSION_COOKIES', $SESSION_COOKIES);
                
              
                //Executing statement
                oci_execute($compiled, OCI_COMMIT_ON_SUCCESS);
            }
            //closing the file
            fclose($myfile);
            $this->updateRegistry($filename);
            return TRUE;
        }
        else{
            throw new Exception("File doesnot exist");
        }
    } 
    
    public function sendEmail(Exception $e){
        
        $sent = mail($this->_config['recipients'], $this->_config['notificationSubject'], $e);
    }
    
    public function updateRegistry($filename)
    {
        
        $uploadedfilename = fopen($this->_config['uploadedRegistry'], "a");
        fwrite($uploadedfilename, basename($filename . date($this->_config['filenameTimeSeparator'] . 'Ymdhi', time())) . PHP_EOL);
    } 
}

try {
    $logAgent = new logAgent();
    $logAgent->uploadLogs();
}
catch (Exception $e) {
    $logAgent->sendEmail($e);
}
?>

0 个答案:

没有答案