如何检查上传的文件类型是pdf [英] how to check the uploaded file type is pdf

查看:225
本文介绍了如何检查上传的文件类型是pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

Possible Duplicate:
How to check file types of uploaded files in PHP?

我在我的网站上载了一个功能,只允许上传PDF,然后如何检查上传的文件是否只有pdf。就像 getimagesize() code>来挑选图像文件。
有没有什么办法检查文件一定是pdf。
我的代码如下所示。

I have uploading feature in my site and only pdf upload is allowed.Than how to check that the uploaded file is pdf only.Just like getimagesize() to ckeck the image files. Is there any way to check the file is sure to be pdf. my code is shown 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";
}

redirect_to mysql_prep 是我定义的函数。
但是mime类型可以使用headers改变。所以有什么办法可以检查文件是否是orignal pdf?

in this redirect_to and mysql_prep are function defined by me. But mime type can be changed using headers.So is there any way to check the file to be orignal pdf?

推荐答案

您可以使用PHP的文件信息函数来检查文件的MIME类型。如果它返回'application / pdf'类型,那么它应该是一个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.

文件信息函数是在PHP 5.3中添加的,但是在之前您可以使用 mime_content_type 功能。

The File Info Functions were added in PHP 5.3 but previous to that you are able to use the mime_content_type function.

这篇关于如何检查上传的文件类型是pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆