View Single Post
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