Replacement for File::mime() in Laravel 4 (to get mime type from file extension)

后端 未结 5 994
名媛妹妹
名媛妹妹 2020-12-06 17:20

Laravel 3 had a File::mime() method which made it easy to get a file\'s mime type from its extension:

$extension = File::extension($path);
$         


        
5条回答
  •  执念已碎
    2020-12-06 18:22

    After reading that:

    • PHP mime_content_type() is deprecated
    • Its replacement FileInfo is unreliable
    • Symfony's getMimeType() uses FileInfo (see my other answer)

    I decided instead to port Laravel 3's implementation of File::mime() into a helper library within my Laravel 4 application. The Laravel 3 implementation just reads the MIME types from a config lookup array, based on file extension.

    Solution:

    • Copied application/config/mimes.php from my L3 project to app/config/mimes.php in my L4 project
    • Made a FileHelper library with the File::mime() function code from the Laravel 3 File class.

提交回复
热议问题