Lampwrights Forum

Lampwrights Forum (http://www.lampwrights.com/index.php)
-   PHP (http://www.lampwrights.com/forumdisplay.php?f=4)
-   -   Scan Files For Viruses Using PHP and ClamAV WITHOUT System Calls (http://www.lampwrights.com/showthread.php?t=47)

Jeff 01-22-2011 07:52 AM

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()));
    }
}



All times are GMT -4. The time now is 04:26 PM.

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