Symfony2: mime_content_type () not recognized

My controller looks like this:

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));
}

      

Passing the path to it results in the following error message:

Call to undefined function MyBundle\Controller\mime_content_type()

      

How can I solve this problem?

+3


source to share


1 answer


Try using File Class

$oFile = new File($filepath);
$response->headers->set('Cache-Control', 'private');
$response->headers->set('Content-type', $oFile->getMimeType());
$response->headers->set('Content-Disposition', 'attachment; filepath="' . $oFile->getBasename() . '";');
$response->headers->set('Content-length', $oFile->getSize());

      

I have not tested running the code. Hope this works great. don't forget to add



use Symfony\Component\HttpFoundation\File\File; 

      

at the top of the file.

Hope it helps.

0


source







All Articles