symfony: stream download

by prettyscripts on 2010-05-17 10:42:06

phpsymfony

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.

Tags: download, php, symfony

No feedback yet

Leave a comment


Your email address will not be revealed on this site.
PoorExcellent
note: all comments are moderated. do not spam and do not advertise. only comments relevant to the post will be published.
(Line breaks become <br />)
(For my next comment on this site)
(Allow users to contact me through a message form -- Your email will not be revealed!)