17

I am trying to do force download using ZF2. Here is the snippet to my code

 use Zend\Http\Request;
 .....
   public function downloadAction() {
     $response = new Request();
    $response->setHeaders(Request::fromString("Content-Type: application/octet-stream\r\nContent-Length: 9\r\nContent-Disposition: attachment; filename=\"ultimate_remedy_readme.txt\""));


}

now i am getting this error

/var/www/whowantsmymoney/vendor/zendframework/zendframework/library/Zend/Http/Request.php:88

Message:

A valid request line was not found in the provided string

Stack trace:

#0 /var/www/whowantsmymoney/module/Admin/src/Admin/Controller/LanguageController.php(93): Zend\Http\Request::fromString('Content-Type: a...')
noobie-php
  • 6,817
  • 15
  • 54
  • 101
  • Instead of instantiating a new request object you should get the existing response. Try $this->getResponse(); after you have set the headers you should return the response. – Aydin Hassan Mar 05 '13 at 13:56
  • the only reasons that i wasnt using the existing headers was, that existing headers didnt had content that i desired so i had to create new. – noobie-php Mar 06 '13 at 10:07

2 Answers2

36

This code should help you for a simple file download.

public function downloadAction() {
    $fileName = 'somefile';

    if(!is_file($fileName)) {
        //do something
    }

    $fileContents = file_get_contents($fileName);

    $response = $this->getResponse();
    $response->setContent($fileContents);

    $headers = $response->getHeaders();
    $headers->clearHeaders()
        ->addHeaderLine('Content-Type', 'whatever your content type is')
        ->addHeaderLine('Content-Disposition', 'attachment; filename="' . $fileName . '"')
        ->addHeaderLine('Content-Length', strlen($fileContents));


    return $this->response;
}

I imagine this code leaves a lot to be desired, but should work in simple cases, as was mine. I'm not sure how you might handle reading the file in chunks. Maybe somebody else could shed some light?

Edit - Sending streams

I've added this here for informational purposes. It is probably the better way to force downloads as it will use much less memory.

public function downloadAction() {
    $fileName = 'somefile';

    $response = new \Zend\Http\Response\Stream();
    $response->setStream(fopen($fileName, 'r'));
    $response->setStatusCode(200);

    $headers = new \Zend\Http\Headers();
    $headers->addHeaderLine('Content-Type', 'whatever your content type is')
            ->addHeaderLine('Content-Disposition', 'attachment; filename="' . $fileName . '"')
            ->addHeaderLine('Content-Length', filesize($fileName));

    $response->setHeaders($headers);
    return $response;
Aydin Hassan
  • 1,465
  • 2
  • 20
  • 41
  • in the first case you return `return $this->response;` in another `return $response;` is it a typo or is it correct ? – Zippp Jun 06 '18 at 11:22
5

Thanks to @Aydin Hassan for response, but several important headers are missing in his answer. Be careful of that.

Full headers stack:

public function downloadAction() {
    $file = 'path/to/file';
    $response = new \Zend\Http\Response\Stream();
    $response->setStream(fopen($file, 'r'));
    $response->setStatusCode(200);
    $response->setStreamName(basename($file));
    $headers = new \Zend\Http\Headers();
    $headers->addHeaders(array(
        'Content-Disposition' => 'attachment; filename="' . basename($file) .'"',
        'Content-Type' => 'application/octet-stream',
        'Content-Length' => filesize($file),
        'Expires' => '@0', // @0, because zf2 parses date as string to \DateTime() object
        'Cache-Control' => 'must-revalidate',
        'Pragma' => 'public'
    ));
    $response->setHeaders($headers);
    return $response;
}
Athlan
  • 6,389
  • 4
  • 38
  • 56
  • Agreed.But you should consider the fact , that this question was asked around 2 years ago and according to the need of that time and maturity of ZF2 at that time, this question and its answered solved alot of basic level problems – noobie-php May 21 '15 at 11:23
  • 3
    @noobie-php You have my guarantee, that HTTP specification doesn't changed during this 2 years. I strive to improve SO community content and added some headers. That headrs prevents eventual caching downloading content, depends on browser's implementation. Thanks for asked question, the answer for that (after some more research) made my problem solved quicker. – Athlan May 22 '15 at 08:34