doudansui6650 2011-10-05 21:42
浏览 113
已采纳

使用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.

  • 写回答

1条回答 默认 最新

  • duankanyi6539 2011-10-05 21:49
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB动图的问题
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名