简体   繁体   English

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

[英]Check mime type of file on upload with PHP. mime_content_type, fileINfo, linux file NOT available

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. 我正在努力帮助客户将一个从另一个主机迁移到godaddy共享主机的站点重新组合在一起。

the site is built using CakePHP and one line uses mime_content_type, which is not available on this server. 该站点使用CakePHP构建,一行使用mime_content_type,该服务器上不可用。 neither is the FILE command and neither is fileinfo. 既不是FILE命令也不是fileinfo。

i tried the upgrade.php replacement, but it fails presumably because it can't find mime_magic. 我尝试了upgrade.php替换,但它失败了大概是因为它找不到mime_magic。

the PEAR solution fails due to FILE not being available. 由于FILE不可用,PEAR解决方案失败。

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. 这里唯一的目标是找出文件是否是PDF,而不是基于扩展名。

PHP Version 5.2.17 PHP版本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. PDF文件始终以%PDF-开头,因此您可以读取前4个字节并检查它们是否与之相等。

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 . 但是, 下载 MIME魔术文件并将其名称赋予finfo_open (php 5.3+)或配置mime_magic.magicfile并使用mime_content_type可能更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM