tags:

views:

38

answers:

3

Hi, I should set up a simple download manager and I don't understand whay this code doesn't work:

protected function doDownload($filename){
        //$this->_helper->layout()->disableLayout();
        //$this->_helper->viewRenderer->setNoRender(true);
        $dir = Zend_Registry::get('dir');
        $file = $dir->assets.$filename;
        if(file_exists($file)){
            $response = $this->getResponse();
            $response->setHeader('Content-Description','File Transfer', true);
            $response->setHeader('Content-Type','application/octet-stream', true);
            $response->setHeader('Content-Disposition','attachment; filename='.basename($file), true);
            $response->setHeader('Content-Transfer-Encoding','binary', true);
            $response->setHeader('Expires','0', true);
            $response->setHeader('Cache-Control','must-revalidate, post-check=0, pre-check=0', true);
            $response->setHeader('Pragma','public', true);
            $response->setHeader('Content-Length: ' , filesize($file),true);
            ob_clean();
            flush();
            readfile($file);
            exit(0);
          /*header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename='.basename($file));
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            ob_clean();
            flush();
            readfile($file);
            exit(0);*/

         }


    }

while with the commented code the script works fine.

Can you help me please ?

Bye.

A: 

Hi, Personally, I do not understand why your code should work? Should readfile() automatically trigger a sendHeaders() or writeHeaders() method on the $response object before the file content is output?!? I think you should call this method or use a method which calls it... like the suggested $this->getResponse->setBody() in the other answer

greg0ire
@Felix KlingIt shows the file instead do download it@greg0ireI never used the response object in this way and I didn't find any tutorial could you show me anexample please ?
whisher
+3  A: 

You basically don't perform any output through the Zend Framework (apart from your headers). Try the following :

if (file_exists($file)) {
    $this->getResponse()
        ->setHeader('Content-Description','File Transfer', true)
        ->setHeader('Content-Type','application/octet-stream', true)
        ->setHeader('Content-Disposition','attachment; filename='.basename($file), true)
        ->setHeader('Content-Transfer-Encoding','binary', true)
        ->setHeader('Expires','0', true)
        ->setHeader('Cache-Control','must-revalidate, post-check=0, pre-check=0', true)
        ->setHeader('Pragma','public', true)
        ->setHeader('Content-Length: ' , filesize($file),true);
    $this->getResponse()->setBody(file_get_contents($file));
}

BTW You will have to comment out your first two lines of code again AND don't us exit(0), otherwise it won't work, since the actual sending of headers and body will be done post-action.

wimvds
Thanks to everybody it works fine.Bye.
whisher
I commented out this line $layout = Zend_Controller_Action_HelperBroker::getStaticHelper('Layout'); $view = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer'); $layout->disableLayout(); $view->setNoRender(true);It is a plugin.
whisher
A: 

It doesn't work yet. At localhost do download but a corrupted file. On the host it returns to index page.

protected function doDownload($filename){
    $dir = Zend_Registry::get('dir');
    $file = $dir->assets.$filename;
    if(file_exists($file)){
       $this->getResponse()
        ->setHeader('Content-Description','File Transfer', true)
        ->setHeader('Content-Type','application/octet-stream', true)
        ->setHeader('Content-Disposition','attachment; filename='.basename($file), true)
        ->setHeader('Content-Transfer-Encoding','binary', true)
        ->setHeader('Expires','0', true)
        ->setHeader('Cache-Control','must-revalidate, post-check=0, pre-check=0', true)
        ->setHeader('Pragma','public', true)
        ->setHeader('Content-Length: ' , filesize($file),true);
        $this->getResponse()->setBody(file_get_contents($file));
        Zend_Controller_Action_HelperBroker::getStaticHelper('Layout')->disableLayout();
       Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true);
     }
     //exit(0);
}

the whole plugin

class App_Controller_Plugin_AssetDownload
extends Zend_Controller_Plugin_Abstract

{ public function dispatchLoopStartup (Zend_Controller_Request_Abstract $request) {

    if ($request->getControllerName() != 'download'){
        return;
    }
    $this->doDownload($request->getActionName());


}

protected function doDownload($filename){
    $dir = Zend_Registry::get('dir');
    $file = $dir->assets.$filename;
    if(file_exists($file)){
       $this->getResponse()
        ->setHeader('Content-Description','File Transfer', true)
        ->setHeader('Content-Type','application/octet-stream', true)
        ->setHeader('Content-Disposition','attachment; filename='.basename($file), true)
        ->setHeader('Content-Transfer-Encoding','binary', true)
        ->setHeader('Expires','0', true)
        ->setHeader('Cache-Control','must-revalidate, post-check=0, pre-check=0', true)
        ->setHeader('Pragma','public', true)
        ->setHeader('Content-Length: ' , filesize($file),true);
        $this->getResponse()->setBody(file_get_contents($file));
        Zend_Controller_Action_HelperBroker::getStaticHelper('Layout')->disableLayout();
       Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true);

     }
     //exit(0);
}

}

whisher