opencart自定义下载文件错误

时间:2014-11-15 14:51:14

标签: opencart opencart2.x

我希望在产品页面中制作下载文件而无需登录或在opencart中注册为公共下载链接,在编码后没有获得正确的下载文件,并且还下载了未知和未格式化的文件。我得到screnshot

enter image description here

模型文件

        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;
    }

控制器文件

$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'));
        }
    }
}

查看文件

<?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 } ?>

1 个答案:

答案 0 :(得分:1)

您必须在控制器文件中进行一些更改。我已经改变了你的代码。只需用下载功能替换以下代码即可。希望这可以提供帮助。

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'));
    }
}`