获取mime类型的文件

时间:2015-07-10 19:54:53

标签: php mime-types

我试图获取他们以user_id.jpg格式或gif或png格式的个人资料图片的mime类型,我尝试使用此代码,但它无法正常工作。

function detectFileMimeType($filename='')
{
    $filename = escapeshellcmd($filename);
    $command = "file -b --mime-type -m /usr/share/misc/magic {$filename}";

    $mimeType = shell_exec($command);

    return trim($mimeType);
}

 function get_avatar($image, $user_id, $account) 
 {
   $imgurl ="http://mypage/files/pictures/picture-" . ($user_id, $mimeType) . ".jpg";

   if (!is_imgurl_good($imgurl)) {
     $imgurl = "http://mypage/sites/all/themes/simple_custom/user.png";
   }
   return $imgurl;
 }

2 个答案:

答案 0 :(得分:0)

您应该可以使用PHP提供的内置函数轻松获取文件的MIME类型 -

class FooFighter { public: virtual void foo() = 0; };

class A { ... };

class B1 : public A { ... };

class B2 : public A, public FooFighter { ... };

...

int main() {
  std::vector<A *> v;

  // fill up v

  for (int i = 0; i < v.size(); ++i) {
    FooFighter * ff = dynamic_cast<FooFighter *>(v[i]);
    if (ff) ff.foo();
  }
  return 0;

http://php.net/manual/en/function.finfo-file.php

http://php.net/manual/en/function.glob.php

答案 1 :(得分:0)

除了工作正常的getimagesize之外,稍微简单的方法是返回PHP $file = "blah/blahblah/bubbles.jpg"; $fileTypeData = getimagesize($file); $fileTypeData['mime'] = The Mime type of the image file. 函数,因为这将返回从文件中提取的MIME类型:

 Bundle bundle = new Bundle();
 bundle.putString("someString", stringValue);
 fragment.putArguments(bundle);