Is using ClamAV for file uploads in PHP a recommended practice for preventing malware infections?
Using ClamAV for file uploads in PHP is a recommended practice for preventing malware infections. ClamAV is an open-source antivirus software that can scan files for malware before allowing them to be uploaded to the server. By integrating ClamAV into your PHP code, you can ensure that any files uploaded by users are safe and free from malicious content.
// Path to ClamAV executable
$clamav_path = '/usr/bin/clamscan';
// Function to scan uploaded file using ClamAV
function scanFileWithClamAV($file_path) {
global $clamav_path;
$output = shell_exec("$clamav_path --no-summary $file_path");
if (strpos($output, 'OK') !== false) {
return true; // File is clean
} else {
return false; // File contains malware
}
}
// Example usage
$file_path = '/path/to/uploaded/file.txt';
if (scanFileWithClamAV($file_path)) {
// File is clean, proceed with upload
move_uploaded_file($_FILES['file']['tmp_name'], $file_path);
} else {
// File contains malware, do not proceed with upload
echo 'File contains malware and cannot be uploaded.';
}