views:

346

answers:

1

I'm trying to make an action that returns a file from disk to the user (via little download dialog popup box.) I've got it working great, except somehow a newline is being inserted as the first character. This breaks jpeg and doc files, for example. If I remove the newline from the downloaded file, the files work.

I've also looked through the boostrap and initializer to make sure there's not any newlines hanging out there. No luck.

Here's what I'm working with: -Zend Framework 1.7

my action:

    public function getFileAction(){
  $file_id = $this->_request->getParam('id');

  //check if the id given is good
  $filem = new UserFileModel();
  $filedata = $filem->getFileByID($file_id);
  $user_id = $filedata['user_id'];
  $file_id = $filedata['file_id'];

  $this->getHelper('viewRenderer')->setNoRender();
  Zend_Layout::getMvcInstance()->disableLayout();

  $str = file_get_contents('..\\userfiles\\'.$user_id.'\\'.$file_id);

  $this->_response->clearBody();
  $this->_response->clearHeaders();
  $this->_response
    ->setHeader('Content-Type', $filedata['file_mimetype']) 
    ->setHeader('Content-Disposition', 'attachment; filename="' . $filedata['file_name'] . '"')
    ->setHeader("Connection", "close")
    ->setHeader("Content-Length", strlen($str))
    ->setHeader("Content-transfer-encoding", "binary")
    ->setHeader("Cache-control", "private")
    ->setBody($str);
}

Do I have something echoing a null variable and giving me nothing but the newline in my other code? I'm going mad trying to get rid of this newline!

Thanks ~vb

+1  A: