getMimeType函数为Symfony上的FileinfoMimeTypeGuesser返回null

时间:2015-08-22 23:13:01

标签: php symfony laravel-5

我收到错误finfo::finfo() expects parameter 2 to be a valid path, string given并且我查看了Symfony Documentation并且它说该函数返回The mime type or NULL, if none could be guessed 但我不明白为什么它找不到哑剧型。 我的路线

Route::get( '/images/{file}', 'ImageController@getImage' );

我的网址是www.example.com/image/logo.png

my ImageController

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;


class ImageController extends Controller {

    public function getImage($filename) {

        $path = \Config::get('assets.images') . $filename;


        $file = new \Symfony\Component\HttpFoundation\File\File($path);
        // Make a new response out of the contents of the file
        // Set the response status code to 200 OK
        $response = \Response::make(
            \File::get($path), 
            200
        );

        // Modify output's header.
        // Set the content type to the mime of the file.
        // In the case of a .jpeg this would be image/jpeg
        $response->header(
            'Content-type',
            $file->getMimeType()
        );
        // We return our image here.
        return $response;
    }

}

$file->getMineType()是问题,当我var_dump($file)我得到object(Symfony\Component\HttpFoundation\File\File)[185] private 'pathName' (SplFileInfo) => string '/home/vagrant/Code/project/storage/assets/images/logo.png' (length=55) private 'fileName' (SplFileInf

我有点困惑为什么png文件类型为null,对于任何其他文件类型也是如此。

我已将问题缩小到错误来自FileBinaryMimeTypeGuesser我不知道magicFile可能是什么。

1 个答案:

答案 0 :(得分:1)

您是否检查过您的php.ini是否在php.ini中启用了fileinfo扩展名

您可以尝试,如果所有文件类型都发生同样的情况,则很可能禁用了扩展名:

;extension=php_fileinfo.dll

我需要被激活,所以:

extension=php_fileinfo.dll

在linux中我认为是

extension=fileinfo.so

PHP Fileinfo functions

中详细了解fileinfo

在php.ini文件中取消注释该行,重新启动apache并测试