Dev Troubleshooter
Dev Troubleshooter

Reputation: 115

File is downloading without extension?

I am trying to download a PDF file but when i click on the download button it downloads the file without extension. That is there is no PDF extension with the file when i download it!

Action:

public function executeDownload(sfWebRequest $request) {
        $this->ebook_id = $request->getParameter('ebook', $this->default_ebook);
        $this->ebook = $this->ebooks[$this->ebook_id];

        if (!is_array($this->ebook))
            return;

        // Allow user to download the ebook (and make sure there is no other output)
        $this->setLayout(false);
        sfConfig::set('sf_web_debug', false);
        $content = sfConfig::get('sf_root_dir').'/web'.$this->ebook['ebook'];
        //echo "<p>PDF: $content</p>"; var_export(filesize($content));exit;
        // Check if the file exists
        $this->forward404Unless(file_exists($content));

        // Record the download for this eBook
        $c = new Criteria();
        $c->add(EbookDownloadsPeer::EBOOK_SLUG, $this->ebook_id);
        $ebook = EbookDownloadsPeer::doSelectOne($c);

        $ebook->setLastDownloaded(time());
        $ebook->setEbookDownloads($ebook->getEbookDownloads()+1);
        $ebook->save();

        // Adding the file to the Response object
        $content_type = (in_array($this->ebook_id, array('readyourbody', 'whyamifatigued', 'nutrientsfromfood'))) ? 'application/pdf': 'image/jpeg';

        $this->getResponse()->clearHttpHeaders();
        $this->getResponse()->setHttpHeader('Pragma: public', true);
        $this->getResponse()->setContentType($content_type);
        $this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename="'.$this->ebook['name'].'"');
        $this->getResponse()->setHttpHeader('Content-Length', filesize($content));
        $this->getResponse()->sendHttpHeaders();
        $this->getResponse()->setContent(readfile($content));

        return sfView::NONE;
    }

Template:

 <div class="cyan3 txt-large txt-c">
      <?php echo link_to('Click here','ebooks/download?ebook='.$ebook_id,'') ?> to <?php echo link_to('download it now','ebooks/download?ebook='.$ebook_id,'') ?>!
        </div><br />
    </div>

Upvotes: 1

Views: 1970

Answers (2)

Mark C.
Mark C.

Reputation: 471

Check the contents of the $this->ebook array and look for a field that holds the file extension. Add this value to the line

$this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename="'.$this->ebook['name'] . '"');

so it because something like

$this->getResponse()->setHttpHeader('Content-Disposition', 'attachment; filename="'.$this->ebook['name'] . $this->ebook['extension'].'"');

Upvotes: 1

Rahul
Rahul

Reputation: 18557

Please append .pdf as extension to your ebook name.
It will work.

Upvotes: 0

Related Questions