Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony2:无法识别mime\u内容\u类型()_Php_Symfony - Fatal编程技术网

Php Symfony2:无法识别mime\u内容\u类型()

Php Symfony2:无法识别mime\u内容\u类型(),php,symfony,Php,Symfony,我的控制器如下所示: public function download($filepath) { // Generate response $response = new Response(); // Set headers $response->headers->set('Cache-Control', 'private'); $response->headers->set('Content-type', mime_conten

我的控制器如下所示:

public function download($filepath)
{
    // Generate response
    $response = new Response();

    // Set headers
    $response->headers->set('Cache-Control', 'private');
    $response->headers->set('Content-type', mime_content_type($filepath));
    $response->headers->set('Content-Disposition', 'attachment; filepath="' . basename($filepath) . '";');
    $response->headers->set('Content-length', filesize($filepath));

    // Send headers before outputting anything
    $response->sendHeaders();

    $response->setContent(readfile($filepath));
}
向其传递文件路径会导致以下错误消息:

Call to undefined function MyBundle\Controller\mime_content_type()
如何解决此问题?

尝试使用

我还没有通过运行代码进行测试。希望这一切顺利。别忘了加上

use Symfony\Component\HttpFoundation\File\File; 
在文件顶部的语句


希望这有帮助。

谢谢。我通过
use
添加了这个类,但是当我想通过
$oFile=new file($filepath)
创建一个文件实例时,PHPStorm告诉我这个类是未定义的。这怎么可能?我忘了什么?@Max请将您的声明与use-Symfony\Component\HttpFoundation\File\File匹配;我也更新了答案。谢谢,这很有效。但我得到的信息是找不到该文件。下面是我如何检索作为上述代码中的参数传递的文件路径:
{%set filepath=asset('bundles/mybundle/files/example.pdf')%}
怎么了?@Max oh!!您正在传递url。您有该文件的文件系统路径吗?如果是,则将$filepath指定为系统路径。(例如C:/wamp/www/project/file.pdf)
use Symfony\Component\HttpFoundation\File\File;