Possible Duplicate:
How to check file types of uploaded files in PHP?
I have uploading features on my site and only PDF uploads are allowed. How can I check that the uploaded file is only a PDF? Just like getimagesize() can verify image files.
Is there any way to check the file is a PDF? My code is below:
$whitelist = array(".pdf");
foreach ($whitelist as $item) {
if (preg_match("/$item\$/i", $_FILES['uploadfile']['name'])) {
}
else {
redirect_to("index.php");
}
}
$uploaddir = 'uploads/';
$uploadfile = mysql_prep($uploaddir . basename($_FILES['uploadfile']['name']));
if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $uploadfile)) {
echo "succussfully uploaded";
}
Functions redirect_to and mysql_prep are defined by me. But mime type can be changed using headers. So is there any way to check the file to be an original pdf?
You can check the MIME type of the file using PHP’s File Info Functions. If it returns with the type ‘application/pdf’ then it should be a PDF.
The File Info Functions were added in PHP 5.3 but previous to that you are able to use the mime_content_type function.