简单的一个条件声明文件信息

时间:2017-03-15 10:51:56

标签: php symfony conditional-statements content-type fileinfo

我没有在网上找到一个好的答案..

我正在使用symfony2.5和php 5.3并创建文件资源管理器应用程序。

我想在应用良好的关联内容类型之前知道文件的扩展名。  不推荐使用mime_content_type函数..

这是我的showAction {},我需要一个函数来测试文件是excel还是pdf文件:

public function showAction($repertoire, $file)
{

    $response = new Response();
    $response->setContent(file_get_contents(''.$repertoire.'/'.$file.''));

    if(file_info($file) == 'application/pdf'){

    $response->headers->set('Content-Type', 'application/pdf');
    $response->headers->set('Content-disposition', 'filename='. $file);
    return $response;

    }else {

    $response->headers->set('Content-Type', 'application/application/vnd.ms-excel');
    $response->headers->set('Content-disposition', 'filename='. $file);
    return $response;
    }
}

我从编程开始。你能帮帮我们吗? 非常感谢!

1 个答案:

答案 0 :(得分:0)

finfo有点奇怪。您必须创建一个finfo资源,然后使用该资源来检查文件,如下所示:

$finfo = finfo_open(FILEINFO_MIME_TYPE);
if('application/pdf' === finfo_file($finfo, $file)) {
    ...
}

或者,OOP方式:

$finfo = new finfo(FILEINFO_MIME_TYPE);
if('application/pdf' === $finfo->file($file)) {
    ...
}