使用PHP检查上传时的mime类型的文件。 mime_content_type,fileINfo,linux文件不可用

i have been scouring the web all day and am unable to find a reasonable solution to this problem.

i am trying to help a client hobble back together a site that was moved from another host to godaddy shared hosting.

the site is built using CakePHP and one line uses mime_content_type, which is not available on this server. neither is the FILE command and neither is fileinfo.

i tried the upgrade.php replacement, but it fails presumably because it can't find mime_magic.

the PEAR solution fails due to FILE not being available.

is there any solution to this?

the only goal here is to find out whether or not a file is a PDF or not, not based on the extension.

PHP Version 5.2.17

thanks so much.

PDF files always start with %PDF-, so you can read the first 4 bytes and check that they're equal to that.

function is_pdf($fn) {
  $f = fopen($fn, 'rb');
  if ($f === false) return false;
  $res = fread($f, 5) == '%PDF-';
  fclose($f);
  return $res;
}

However, it may be easier just to download the MIME magic file, and give its name to finfo_open (php 5.3+) or configure mime_magic.magicfile and use mime_content_type.