我的控制器看起来像这样:
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()
我该如何解决这个问题?
答案 0 :(得分:0)
尝试使用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());
我没有通过运行代码进行测试。希望这很好。别忘了添加
use Symfony\Component\HttpFoundation\File\File;
文件顶部的
语句。
希望这有帮助。