Danizord Danizord - 1 year ago 283
PHP Question

Zend Framework 2 file download

I made a form to upload files to the folder

using the

This is working like a charm. My problem now is how do I provide this file to users download it?

I think it would be something like:


But I want to know what is the best way to do this.

Answer Source

Thanks to @henrik 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'));
    $headers = new \Zend\Http\Headers();
        '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'
    return $response;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download