by prettyscripts on 2010-05-17 10:42:06
to download file the symfony way, add a download action and not to display layout unless there are errors.
in /path/to/your/app/modules/your_module/actions/actions.class.php:
PHP:
public function executeDownload(sfWebRequest $request) { | |
$file = Doctrine::getTable('File')->find($request->getParameter('id')); | |
| |
// check if file exists | |
if (!$file->fileUploaded()) { | |
$this->getUser()->setFlash('error', 'File does not exist.'); | |
return sfView::ERROR; | |
} | |
| |
// do any file information formatting | |
// do any credential validation | |
| |
// dowload file | |
$response = $this->getContext()->getResponse(); | |
$response->clearHttpHeaders(); | |
$response->addCacheControlHttpHeader('Cache-control', 'must-revalidate, post-check=0, pre-check=0'); | |
$response->setContentType('application/octet-stream', true); | |
$response->setHttpHeader('Content-transfer-encoding', 'binary'); | |
$response->setHttpHeader('Content-Disposition', 'attachement; filename=' . $file->getSlug() . '.' . $file->getExtension()); | |
$response->sendHttpHeaders(); | |
$response->setContent(file_get_contents($file->getFullPathFilename())); | |
| |
return sfView::NONE; | |
} |
note that this assumes there's a file table in database storing information about this file.