将Apache日志文件上传到oracle数据库时如何丢弃图像

时间:2018-08-06 08:12:35

标签: php oracle apache

我设计了一个脚本,该脚本会将日志文件数据上传到oracle数据库。但是我想在数据上传中过滤图像。在这种情况下,我不希望我的代码上传请求中具有.gif扩展名的CLIENT_REQUEST(“ GET /icons/back.gif HTTP / 1.1”)。有人可以帮我提供代码。

我的LOG文件:-

127.0.0.1,-,-,[06/Aug/2018:09:22:02 +0200],"GET /icons/back.gif HTTP/1.1",304,-,"-"
127.0.0.1,-,-,[06/Aug/2018:09:22:02 +0200],"GET /icons/blank.gif HTTP/1.1",304,-,"-"
127.0.0.1,-,-,[06/Aug/2018:09:22:02 +0200],"GET /icons/unknown.gif HTTP/1.1",304,-,"-"
127.0.0.1,-,-,[06/Aug/2018:09:22:02 +0200],"GET /icons/image2.gif HTTP/1.1",304,-,"-"
127.0.0.1,-,-,[06/Aug/2018:09:22:02 +0200],"GET /icons/text.gif HTTP/1.1",304,-,"-"

我的php代码

<?php
$conn = oci_connect('XYZ', 'XYZxyz1', 'abcdef1/ABC');
if (!$conn) {
   $m = oci_error();
   echo $m['message'], "\n";
   exit;
}
else {

$d = new DateTime();
$yesterday = $d->sub(new DateInterval('P1D'))->format('Y.m.d');
$filename = "access.$yesterday.txt";


$myfile = fopen($filename, "r") or die("Unable to open file!");
while(!feof($myfile)) {
    $content= fgets($myfile);
    $carray=explode(',',$content);
    list($IP_ADDRESS, $USER_IDENTIFIER, $USERID , $REQUEST_TIME , $CLIENT_REQUEST ,$RESPONSE_CODE ,$SIZEOFOBJECT, $COOKIES)=$carray;

    $stdii = 'INSERT INTO LOGS(IP_ADDRESS, USER_IDENTIFIER, USERID , REQUEST_TIME , CLIENT_REQUEST ,RESPONSE_CODE ,SIZEOFOBJECT, COOKIES)'.
    'values(:IP_ADDRESS, :USER_IDENTIFIER, :USERID , :REQUEST_TIME , :CLIENT_REQUEST ,:RESPONSE_CODE ,:SIZEOFOBJECT, :COOKIES)';
    $compiled1 = oci_parse($conn, $stdii);
    oci_bind_by_name($compiled1, ':IP_ADDRESS', $IP_ADDRESS);
    oci_bind_by_name($compiled1, ':USER_IDENTIFIER', $USER_IDENTIFIER);
    oci_bind_by_name($compiled1,':USERID', $USERID);
    oci_bind_by_name($compiled1, ':REQUEST_TIME', $REQUEST_TIME);
    oci_bind_by_name($compiled1, ':CLIENT_REQUEST', $CLIENT_REQUEST);
    oci_bind_by_name($compiled1, ':RESPONSE_CODE', $RESPONSE_CODE);
    oci_bind_by_name($compiled1, ':SIZEOFOBJECT', $SIZEOFOBJECT);
    oci_bind_by_name($compiled1, ':COOKIES', $COOKIES);
    oci_execute($compiled1, OCI_COMMIT_ON_SUCCESS);
}
}
echo "File Uploaded";
oci_close($conn);
fclose($myfile);
?>

1 个答案:

答案 0 :(得分:0)

如果我从您的评论中正确理解,您希望将除图像(gif,jpeg等)之外的所有内容添加到LOGS表中。有几种方法可以实现这一目标。

进行字符串长度检查。这样会从日志中删除检查值,如果它与原始值不匹配,则说明您有图像。

while() {
    if (strlen(str_replace(['.gif', '.jpeg'], '', $log)) !== strlen($log)) {
        // Found an image
        continue;
    }

    // Insert
}

或执行正则表达式检查,以在日志中搜索任何匹配的字符串。

while() {
     if (preg_match("/(.*)(gif|jpeg)(.*)/", $log);) {
         // Found an image
         continue;
     }

     // Insert
}

或迭代一个数组,并使用strpos检查日志中是否包含任何禁止的字符串。

$bannedStrings = ['.gif', '.jpeg'];

while() {
    foreach ($bannedStrings as $string) {
        if (strpos($log, $string) !== false) {
            // Found a banned string
            continue;
        }
    }

    // Insert
}

Stackoverflows编辑器不断弄乱我要添加的文档的链接,因此放弃了。

  1. How do I check if a string contains a specific word?
  2. http://php.net/manual/en/function.preg_match.php
  3. http://php.net/manual/en/function.str_replace.php
  4. http://php.net/manual/en/function.strlen.php
  5. http://php.net/manual/en/function.strpos.php