0

In my project there is the possibility to upload a file; I developed this reading the Symfony2 documentation.

I didn't fine instruction to create an action in the controller able to download those files.

How could I do?

Gianni Alessandro
  • 860
  • 6
  • 11
  • 28

2 Answers2

1

My Version looks like this:

/**
 * @Route("/download/{file_id}", name="download_file")
 */
public function downloadAction($file_id)
{
    // get your filepath from db somehow by file_id or whatever
    $path = ...

    $file = getimagesize($path);

    $response = new Response();
    $response->setContent(file_get_contents($path));
    $response->headers->set('Content-Type', $file['mime']);
    $response->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"');

    return $response;
}
Markus Kottländer
  • 8,228
  • 4
  • 37
  • 61
  • In my project, the uploadDir depends from the file. To define the path, i have to call the method getUploadDir() inside of my object. But the method is protected. Is it sure to make it public? Or, otherwise, how could I do? – Gianni Alessandro Feb 12 '14 at 13:53
0
$response=new Response();
$response->setContent(file_get_contents($localFilePath));

For forcefully download you may use this:

$file_url = 'http://www.myremoteserver.com/file.exe';
header('Content-Type: application/octet-stream');
header("Content-Transfer-Encoding: Binary"); 
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\""); 
readfile($file_url); // do the double-download-dance (dirty but worky)
hizbul25
  • 3,829
  • 4
  • 26
  • 39
  • In my project, the uploadDir depends from the file. To define the path, i have to call the method getUploadDir() inside of my object. But the method is protected. Is it sure to make it public? Or, otherwise, how could I do? – Gianni Alessandro Feb 13 '14 at 13:01