Kundan SIngh
Kundan SIngh

Reputation: 738

opencart custom download file error

i am looking to make download files in product page without login or registration in opencart as public download link, after doing the coding am not getting the right download file and also a unknow and unformatted file is downloaded. what i get screnshot

enter image description here

model file

        public function getProductDownloads($product_id) {
$query = $this->db->query("SELECT d.download_id, d.filename, d.mask, dd.name FROM " . DB_PREFIX . "download d LEFT JOIN " . DB_PREFIX . "download_description dd USING ( download_id ) LEFT JOIN " . DB_PREFIX . "product_to_download p2d USING ( download_id ) WHERE p2d.product_id = '" . (int)$product_id . "'");

    return $query->rows;
    }


        public function getDownload($download_id) {
        $query = $this->db->query("SELECT d.filename, d.mask, dd.name FROM " . DB_PREFIX . "download d LEFT JOIN " . DB_PREFIX . "download_description dd ON (d.download_id = dd.download_id) where d.download_id AND dd.download_id =  '" . (int)$download_id . "'");
return $query->rows;
    }

controller file

$data['downloads'] = array();

            $results = $this->model_catalog_product->getProductDownloads($this->request->get['product_id']);

            foreach ($results as $result) {
                $data['downloads'][] = array(
                'filename'         => $result['filename'],
                'name'         => $result['name'],
                'href'       => $this->url->link('product/product/download', 'download_id=' . $result['download_id'], 'SSL')
                );
            }




/*download*/
    public function download() {

        $this->load->model('catalog/product');

        if (isset($this->request->get['download_id'])) {
            $download_id = $this->request->get['download_id'];
        } else {
            $download_id = 0;
        }

        $download_info = $this->model_catalog_product->getDownload($download_id);

        if ($download_info) {
            $file = DIR_DOWNLOAD . $download_info['filename'];
            $mask = basename($download_info['mask']);

            if (!headers_sent()) {
                if (file_exists($file)) {
                    header('Content-Type: application/octet-stream');
                    header('Content-Disposition: attachment; filename="' . ($mask ? $mask : basename($file)) . '"');
                    header('Expires: 0');
                    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                    header('Pragma: public');
                    header('Content-Length: ' . filesize($file));

                    if (ob_get_level()) {
                        ob_end_clean();
                    }

                    readfile($file, 'rb');

                    exit();
                } else {
                    exit('Error: Could not find file ' . $file . '!');
                }
            } else {
                exit('Error: Headers already sent out!');
            }
        } else {
            $this->response->redirect($this->url->link('common/home', '', 'SSL'));
        }
    }
}

view file

<?php foreach ($downloads as $download) { ?>
<?php echo $download['filename']; ?><br/>
<?php echo $download['name']; ?><br/>
<a href="<?php echo $download['href']; ?>">download</a><br/>
                      <?php } ?>

Upvotes: 0

Views: 595

Answers (1)

user2520441
user2520441

Reputation: 11

You have to make some changes in controller file. I have changed your code. Just replace following code with download function. Hope this could help.

public function download() {

    $this->load->model('catalog/product');

    if (isset($this->request->get['download_id'])) {
        $download_id = $this->request->get['download_id'];
    } else {
        $download_id = 0;
    }

    $download_info = $this->model_catalog_product->getDownload($download_id);

    if ($download_info) {
        $file = DIR_DOWNLOAD . $download_info[0]['filename'];
        $mask = basename($download_info[0]['mask']);

        if (!headers_sent()) {
            if (file_exists($file)) {
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename="' . ($mask ? $mask : basename($file)) . '"');
                header('Expires: 0');
                header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                header('Pragma: public');
                header('Content-Length: ' . filesize($file));

                if (ob_get_level()) {
                    ob_end_clean();
                }

                readfile($file, 'rb');

                exit();
            } else {
                exit('Error: Could not find file ' . $file . '!');
            }
        } else {
            exit('Error: Headers already sent out!');
        }
    } else {
        $this->response->redirect($this->url->link('common/home', '', 'SSL'));
    }
}`

Upvotes: 1

Related Questions