PHP中的图像和PDF文件上载验证

PHP中的图像和PDF文件上载验证,php,image,validation,pdf,Php,Image,Validation,Pdf,我正在创建一个管理员工记录的web应用程序,其中一个功能允许员工上传其资格证书的副本。因为许多扫描仪输出到不同的文件类型,我需要能够验证上传的文件是PDF还是多种不同的图像文件类型之一 我知道MIME验证方法,并使用过它,但由于它相对容易破解,我正在寻找一种更安全的方法来补充它 存储常量的配置文件中的相关代码 // Set the file types allowed to be uploaded define('FILETYPEALLOWED', serialize (array ('imag

我正在创建一个管理员工记录的web应用程序,其中一个功能允许员工上传其资格证书的副本。因为许多扫描仪输出到不同的文件类型,我需要能够验证上传的文件是PDF还是多种不同的图像文件类型之一

我知道MIME验证方法,并使用过它,但由于它相对容易破解,我正在寻找一种更安全的方法来补充它

存储常量的配置文件中的相关代码

// Set the file types allowed to be uploaded
define('FILETYPEALLOWED', serialize (array ('image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png' ) ));
处理表单的代码

    //If a file was uploaded, Move the uploaded file
    IF (isset($_FILES['upload'])) {

      $allowed = unserialize (FILETYPEALLOWED);

      $fileinfo = finfo_open (FILEINFO_MIME_TYPE);

      //IF file type is allowed
      IF (in_array($_FILES['upload'] ['type'], $allowed )) {

        move_uploaded_file($_FILES['upload'] ['tmp_name'], UPLOADS . $_FILES['upload'] ['name'] );

      } else { 
         //Add an error to the $errors array
        $errors[] = 'The file type is not allowed.';
      }

    //Delete the temporary file
    IF (file_exists ($_FILES['upload'] ['tmp_name']) && is_file($_FILES ['upload'] ['tmp_name'])) {
        unlink ($_FILES ['upload'] ['tmp_name']);
        echo '<p>The temp file been file.</p>';
    }
} else {

    print '<p>no file was uploaded</p>';
}   
//如果上载了文件,请移动上载的文件
如果(isset($_文件['upload'])){
$allowed=未序列化(FILETYPEALLOWED);
$fileinfo=finfo\u open(fileinfo\u MIME\u类型);
//如果文件类型是允许的
IF(在_数组中($_文件['upload']['type'],允许使用$){
移动上传的文件($上传文件['upload']['tmp\u name'],上传。$上传文件['upload']['name']);
}否则{
//将错误添加到$errors数组
$errors[]=“不允许使用该文件类型。”;
}
//删除临时文件
如果(文件U存在($文件['upload']['tmp文件名]])和&is文件($文件['upload']['tmp文件名]])){
取消链接($_文件['upload']['tmp_name']);
回显“临时文件已保存到临时文件。

”; } }否则{ 打印“未上载任何文件”

; }
访问此页面:谢谢,我已经读过了,但除非我有误解,否则它只显示文件扩展名的验证,因此我可能会上载扩展名更改为.jpeg的恶意文件,并通过验证测试。我正在寻找一些更健壮的东西访问此:谢谢,我已经读过了,但除非我误解它只显示文件扩展名的验证,因此我可能上载扩展名更改为.jpeg的恶意文件,它将通过验证测试。我在找更强壮的东西