An example of using the system to call the file command on a linux server. This script detects whether a user posted file is a jpeg, gif or png
<?PHP
$accepted_types=array("JPEG" , "GIF", "PNG");
if(!empty($_FILES["uploadedfile"]))
{
$uploaddir = $_SERVER['DOCUMENT_ROOT']."/images/";
$uploaddir.=basename( $_FILES['uploadedfile']['name']);
$last_line = system('file '.escapeshellarg($_FILES['uploadedfile']['tmp_name']), $retval);
$splitvals=explode(' image data' , $last_line);
$vals=explode(':', $splitvals[0]);
$vals[1]=str_replace(' ','', $vals[1]);
if (in_array($vals[1], $accepted_types))
{
echo $vals[1].' was accepted <br />';
if(!file_exists($uploaddir)){
if(move_uploaded_file($_FILES["uploadedfile"]["tmp_name"], $uploaddir))
{
echo $uploaddir." was uploaded! <br />";
}
else
{
echo "There was a problem when uploding the new file, please contact admin about this.";
}
}
else echo 'This file already exists in DB please rename file before uploading';
}
}else echo $_FILES['uploadedfile']['error'].'<br />';
?>