【问题标题】:Easy one condition statement file info简单的一个条件语句文件信息
【发布时间】:2017-08-06 01:37:08
【问题描述】:

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

我正在使用 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;
    }
}

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

【问题讨论】:

  • 必须返回响应对象。
  • 我的条件不起作用,已编辑后
  • 为什么不测试文件扩展名?
  • 应用程序/pdf 不带引号?它不能工作,你应该有语法错误。
  • 我需要帮助来创建我的功能/条件家伙

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


【解决方案1】:

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)) {
    ...
}

【讨论】:

  • 1) 调用未定义函数 'finfo_open' 和 2) 未找到类 'finfo'
  • 我已经在我的 php.ini 中激活了 php_fileinfo.dll 扩展,它工作得很好!谢谢@hair raisin
猜你喜欢
  • 1970-01-01
  • 2016-01-11
  • 2016-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-10
  • 2014-07-12
  • 2015-06-09
相关资源
最近更新 更多