Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9164293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:38:52+00:00 2026-06-17T14:38:52+00:00

Of cource some other people have discussed these problem on stackoverflow, but not all

  • 0

Of cource some other people have discussed these problem on stackoverflow, but not all ansers works for me and often they do not provide a version of there symfony installation.

Topics I read:

  • Send attachment/Download file from Symfony action
  • How to download a file on clicking thefile path using PHP-Symfony?
  • symfony: setHttpHeader() doesn't work, header() does

Thats the point for me to ask how you handle file downloads in symfony 1.4 (without using the view)? In all my use cases I need a template file to render the response. If I send the response due the controller there is the only possibility to send it without an php error (header already sent) with

controller:

/** @var $response sfWebResponse */
$response = $this->getResponse();
$response->clearHttpHeaders();
$response->setContentType($mimeType);
$response->setHttpHeader('Content-Disposition', 'attachment; filename="' . basename($filePath) . '"');
$response->setHttpHeader('Content-Description', 'File Transfer');
$response->setHttpHeader('Content-Transfer-Encoding', 'binary');
$response->setHttpHeader('Content-Length', filesize($filePath));
$response->setHttpHeader('Cache-Control', 'public, must-revalidate');
$response->setHttpHeader('Pragma', 'public');
$response->sendHttpHeaders();

readfile($filePath); die();

This works without an template file. But imho this is not so pretty coding.

The alternative way with the template:

controller:

 /** @var $response sfWebResponse */
$response = $this->getResponse();
$response->clearHttpHeaders();
$response->setContentType($mimeType);
$response->setHttpHeader('Content-Disposition', 'attachment; filename="' . basename($filePath) . '"');
$response->setHttpHeader('Content-Description', 'File Transfer');
$response->setHttpHeader('Content-Transfer-Encoding', 'binary');
$response->setHttpHeader('Content-Length', filesize($filePath));
$response->setHttpHeader('Cache-Control', 'public, must-revalidate');
$response->setHttpHeader('Pragma', 'public');
$response->setContent(file_get_contents($filePath));
$response->sendHttpHeaders();

return sfView::NONE;

view:

<?php echo $sf_response->getRawValue()->getContent(); ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:38:53+00:00Added an answer on June 17, 2026 at 2:38 pm

    My prefered solution

    $filePath = $document->getAbsoluteFilePath();
    $mimeType = mime_content_type($filePath);
    
    /** @var $response sfWebResponse */
    $response = $this->getResponse();
    $response->clearHttpHeaders();
    $response->setContentType($mimeType);
    $response->setHttpHeader('Content-Disposition', 'attachment; filename="' . basename($filePath) . '"');
    $response->setHttpHeader('Content-Description', 'File Transfer');
    $response->setHttpHeader('Content-Transfer-Encoding', 'binary');
    $response->setHttpHeader('Content-Length', filesize($filePath));
    $response->setHttpHeader('Cache-Control', 'public, must-revalidate');
    // if https then always give a Pragma header like this  to overwrite the "pragma: no-cache" header which
    // will hint IE8 from caching the file during download and leads to a download error!!!
    $response->setHttpHeader('Pragma', 'public');
    //$response->setContent(file_get_contents($filePath)); # will produce a memory limit exhausted error
    $response->sendHttpHeaders();
    
    ob_end_flush();
    return $this->renderText(readfile($filePath));
    

    No need of use a template file. Usage of the symfony standard behaviour. Important: The template file must present!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a memory overwrite problem. At some point, during the course of our
i'm curious how other people do this task. Let's say you have like 100+
I have some rake tasks that run each minute and of course that spawns
I am first day with SOLR, and of course, i have some problems with
First some background: The company I work for have decided to create an iPhone
Edit: It seems most people misunderstood my question. I know how enum works, and
I can obviously do some limited testing of my own, but I'm hoping to
I've been away from marking up sites for some time. So, now we have
This question has probably been asked before, but I am not able to find
Okay, I have the weirdest problem. (Warning: Wall of text ahead) I'm tracking a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.