Lampwrights Forum > Linux, Apache, MySQL, PHP > PHP

Reply
 
Thread Tools
01-22-2011, 07:52 AM   #1
Jeff
Administrator
 
Jeff's Avatar
 
Join Date: Jul 2010
Posts: 402
Rep Power: 10
Jeff is getting browny points
Scan Files For Viruses Using PHP and ClamAV WITHOUT System Calls

If your server has clamav installed as a daemon (a service) you can use that to scan files from PHP (or any other langauge). This is useful for when you accept uploads from users or for many other uses.

Here is a function that will allow you to do just that. Just make sure you path the complete path and file name to the function (see comment):

Code:
if (ScanFile($_FILES['attachement']['tmp_name']))
{
            ## Infected!
}

## $file is the complete path to the file, example: /home/site/public_html/file.zip

function ScanFile($file, $clamserver='localhost', $clamavport='3310')
{
    if ($sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))
    {
        if (socket_connect($sock, $clamserver, intval($clamavport)))
        {
            $in = "SCAN " . $file;        
    
            socket_write($sock, $in, strlen($in));

            while ($out = socket_read($sock, 2048)) 
            {
                $results .= $out;
            }
            
            $results = explode(': ', $results);

            if (strtolower(trim($results[1])) != 'ok') 
            { 
                ## Infected!
                return $results[1];
            }

            else
            {
                ## Clean
                return false;
            }
        }
        
        else 
        {
             die('Could not connect to clamd server: ' . socket_strerror(socket_last_error($sock)));
        }
    }

    else 
    {
        die('Could not open a socket: ' . socket_strerror(socket_last_error()));
    }
}
Jeff is offline   Reply With Quote

Reply

Tags

other php

,

php and apache

,

php examples

,

php how-to

,

php security


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



All times are GMT -4. The time now is 07:21 AM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2024, vBulletin Solutions, Inc.