如何在opencart中插入上传图片?

时间:2013-04-02 09:54:49

标签: php opencart

我是OpenCart eshop的初学者。我正在尝试根据打印服务创建自定义表单。我需要将上传的图像插入MySQL并在OpenCart的管理部分显示。如何在OpenCart中验证<input type='file'>文件类型输入?在纯PHP中,$_FILES[][]数组适用,但如何在OpenCart中访问提交的文件?拜托,帮我解决一下。

if (isset($this->error['file'])) {
    $this->data['error_file'] = $this->error['file'];
} else {
    $this->data['error_file'] = '';
}

我的自定义页面控制器:

    <?php
    class ControllerInformationDesign extends Controller {
        private $error = array();

        public function index() {
            /*if (!$this->customer->isLogged()) {
                $this->session->data['redirect'] = $this->url->link('account/custreview', '', 'SSL');

                $this->redirect($this->url->link('account/login', '', 'SSL'));
            } */

            $this->language->load('information/design');

            $this->document->setTitle($this->config->get('config_name') . ' - ' . $this->language->get('heading_title'));

            $this->load->model('account/custreview');

            $this->getForm();
        } 

        public function insert() {
            $this->load->language('information/design');

            $this->document->setTitle($this->config->get('config_name') . ' - ' . $this->language->get('heading_title'));

            $this->load->model('account/custreview');

            if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
                $this->model_account_custreview->addReview($this->request->post);

                $this->session->data['success'] = $this->language->get('text_success');

                $this->redirect($this->url->link('information/design'));
            }

            $this->getForm();
        }

        private function getForm() {
            $this->data['heading_title'] = $this->language->get('heading_title');
            $this->data['entry_prefix'] = $this->language->get('entry_prefix');
            $this->data['entry_desc'] = $this->language->get('entry_desc');
            $this->data['entry_industry'] = $this->language->get('entry_industry');
            $this->data['entry_design'] = $this->language->get('entry_design');
            $this->data['entry_prefix1'] = $this->language->get('entry_prefix1');
            $this->data['entry_other'] = $this->language->get('entry_other');
            $this->data['entry_prefix2'] = $this->language->get('entry_prefix2');
            $this->data['entry_prefix3'] = $this->language->get('entry_prefix3');
            $this->data['entry_prefix4'] = $this->language->get('entry_prefix4');

            $this->data['logged'] = $this->customer->getFirstname() . ' ' . $this->customer->getLastName();

            $this->data['text_none'] = $this->language->get('text_none');
            $this->data['text_select'] = $this->language->get('text_select');

            $this->data['entry_product'] = $this->language->get('entry_product');
            $this->data['entry_author'] = $this->language->get('entry_author');
            $this->data['entry_rating'] = $this->language->get('entry_rating');
            $this->data['entry_review'] = $this->language->get('entry_review');
            $this->data['entry_good'] = $this->language->get('entry_good');
            $this->data['entry_bad'] = $this->language->get('entry_bad');

            $this->data['button_save'] = $this->language->get('continue_shop');
    $this->data['button_checkout'] = $this->language->get('check_out');

            if (isset($this->error['warning'])) {
                $this->data['error_warning'] = $this->error['warning'];
            } else {
                $this->data['error_warning'] = '';
            }

            if (isset($this->session->data['success'])) {
                $this->data['success'] = $this->session->data['success'];

                unset($this->session->data['success']);
            } else {
                $this->data['success'] = '';
            }

            if (isset($this->error['product'])) {
                $this->data['error_product'] = $this->error['product'];
            } else {
                $this->data['error_product'] = '';
            }

            if (isset($this->error['product_id'])) {
                $this->data['error_product_id'] = $this->error['product_id'];
            } else {
                $this->data['error_product_id'] = '';
            }

            if (isset($this->error['author'])) {
                $this->data['error_author'] = $this->error['author'];
            } else {
                $this->data['error_author'] = '';
            }

            if (isset($this->error['text'])) {
                $this->data['error_text'] = $this->error['text'];
            } else {
                $this->data['error_text'] = '';
            }

            if (isset($this->error['rating'])) {
                $this->data['error_rating'] = $this->error['rating'];
            } else {
                $this->data['error_rating'] = '';
            }   
            if (isset($this->error['cdesc'])) {
                $this->data['error_desc'] = $this->error['cdesc'];
            } else {
                $this->data['error_desc'] = '';
            }
            if (isset($this->error['industry'])) {
                $this->data['error_industry'] = $this->error['industry'];
            } else {
                $this->data['error_industry'] = '';
            }
            if (isset($this->error['design'])) {
                $this->data['error_design'] = $this->error['design'];
            } else {
                $this->data['error_design'] = '';
            }
            if (isset($this->error['other'])) {
                $this->data['error_other'] = $this->error['other'];
            } else {
                $this->data['error_other'] = '';
            }
            if (isset($this->error['file'])) {
                $this->data['error_file'] = $this->error['file'];
            } else {
                $this->data['error_file'] = '';
            }

            $this->data['breadcrumbs'] = array();

            $this->data['breadcrumbs'][] = array(
                'text'      => $this->language->get('text_home'),
                'href'      => $this->url->link('common/home', 'SSL'),
                'separator' => false
            );

            $this->data['breadcrumbs'][] = array(
                'text'      => $this->language->get('heading_title'),
                'href'      => $this->url->link('information/design','SSL'),
                'separator' => ' :: '
            );

            if (!isset($this->request->get['review_id'])) { 
                $this->data['action'] = $this->url->link('information/design/insert');
            }

            $this->data['cancel'] = $this->url->link('information/design','SSL');

            if (isset($this->request->get['review_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
                $review_info = $this->model_account_custreview->getReview($this->request->get['review_id']);
            }

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

            $this->data['categories'] = $this->model_catalog_category->getCategories(0);

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

            if (isset($this->request->post['product_id'])) {
                $this->data['product_id'] = $this->request->post['product_id'];

                $product_info = $this->model_catalog_product->getProduct($this->request->post['product_id']);

                if ($product_info) {
                    $this->data['product'] = $product_info['name'];
                } else {
                    $this->data['product'] = '';
                }
            } elseif (isset($review_info)) {
                $this->data['product_id'] = $review_info['product_id'];

                $product_info = $this->model_catalog_product->getProduct($review_info['product_id']);

                if ($product_info) {
                    $this->data['product'] = $product_info['name'];
                } else {
                    $this->data['product'] = '';
                }
            } else {
                $this->data['product_id'] = '';
            }

            if (isset($this->request->post['product_id'])) {
                $this->data['product_id'] = $this->request->post['product_id'];
            } elseif (isset($review_info)) {
                $this->data['product_id'] = $review_info['product_id'];
            } else {
                $this->data['product_id'] = '';
            }

            if (isset($this->request->post['product'])) {
                $this->data['product'] = $this->request->post['product'];
            } elseif (isset($review_info)) {
                $this->data['product'] = $review_info['product'];
            } else {
                $this->data['product'] = '';
            }

            if (isset($this->request->post['author'])) {
                $this->data['author'] = $this->request->post['author'];
            } elseif (isset($review_info)) {
                $this->data['author'] = $review_info['author'];
            } else {
                $this->data['author'] = '';
            }

            if (isset($this->request->post['text'])) {
                $this->data['text'] = $this->request->post['text'];
            } elseif (isset($review_info)) {
                $this->data['text'] = $review_info['text'];
            } else {
                $this->data['text'] = '';
            }

            if (isset($this->request->post['rating'])) {
                $this->data['rating'] = $this->request->post['rating'];
            } elseif (isset($review_info)) {
                $this->data['rating'] = $review_info['rating'];
            } else {
                $this->data['rating'] = '';
            }

            if (isset($this->request->post['cdesc'])) {
                $this->data['cdesc'] = $this->request->post['cdesc'];
            } elseif (isset($review_info)) {
                $this->data['cdesc'] = $review_info['cdesc'];
            } else {
                $this->data['cdesc'] = '';
            }
            if (isset($this->request->post['industry'])) {
                $this->data['industry'] = $this->request->post['industry'];
            } elseif (isset($review_info)) {
                $this->data['industry'] = $review_info['industry'];
            } else {
                $this->data['industry'] = '';
            }
            if (isset($this->request->post['design'])) {
                $this->data['design'] = $this->request->post['design'];
            } elseif (isset($review_info)) {
                $this->data['design'] = $review_info['design'];
            } else {
                $this->data['design'] = '';
            }
            if (isset($this->request->post['other'])) {
                $this->data['other'] = $this->request->post['other'];
            } elseif (isset($review_info)) {
                $this->data['other'] = $review_info['other'];
            } else {
                $this->data['other'] = '';
            }
            if (isset($this->request->post['file'])) {
                $this->data['file'] = $this->request->post['file'];
            } elseif (isset($review_info)) {
                $this->data['file'] = $review_info['file'];
            } else {
                $this->data['file'] = '';
            }

            if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/information/design.tpl')) {
                $this->template = $this->config->get('config_template') . '/template/information/design.tpl';
            } else {
                $this->template = 'default/template/information/design.tpl';
            }

            $this->children = array(
                'common/column_left',
                'common/column_right',
                'common/content_top',
                'common/content_bottom',
                'common/footer',
                'common/header'     
            );


            $this->response->setOutput($this->render());
        }

        private function validateForm() {

            /*if (!$this->request->post['product_id']) {
                $this->error['product'] = $this->language->get('error_product');
            }

            if (!$this->request->post['product_id']) {
                $this->error['product_id'] = $this->language->get('error_product_id');
            }*/

            if ((strlen(utf8_decode($this->request->post['author'])) < 3) || (strlen(utf8_decode($this->request->post['author'])) > 64)) {
                $this->error['author'] = $this->language->get('error_author');
            }
    /*
            if (strlen(utf8_decode($this->request->post['text'])) < 1) {
                $this->error['text'] = $this->language->get('error_text');
            }*/

            /*if (!isset($this->request->post['rating'])) {
                $this->error['rating'] = $this->language->get('error_rating');
            }
            if (!isset($this->request->post['file'])) {
                $this->error['file'] = $this->language->get('error_file');
            }*/
            if ((strlen(utf8_decode($this->request->post['cdesc'])) < 3) || (strlen(utf8_decode($this->request->post['cdesc'])) > 1000)) {
                $this->error['cdesc'] = $this->language->get('error_desc');
            }
            if ((strlen(utf8_decode($this->request->post['industry'])) < 3) || (strlen(utf8_decode($this->request->post['industry'])) > 1000)) {
                $this->error['industry'] = $this->language->get('error_industry');
            }
        if ((strlen(utf8_decode($this->request->post['design'])) < 3) || (strlen(utf8_decode($this->request->post['design'])) > 1000)) {
            $this->error['design'] = $this->language->get('error_design');
        }
        if ((strlen(utf8_decode($this->request->post['other'])) < 3) || (strlen(utf8_decode($this->request->post['other'])) > 1000)) {
            $this->error['other'] = $this->language->get('error_other');
        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }   

    public function autocomplete() {
        $json = array();

        if (isset($this->request->post['filter_name'])) {
            $this->load->model('catalog/product');

            $data = array(
                'filter_name' => $this->request->post['filter_name'],
                'start'       => 0,
                'limit'       => 20
            );

            $results = $this->model_catalog_product->getProducts($data);

            foreach ($results as $result) {
                $json[] = array(
                    'product_id' => $result['product_id'],
                    'name'       => html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8'),   
                    'model'      => $result['model'],
                    'price'      => $result['price']                
                );  
            }
        }

        $this->load->library('json');

        $this->response->setOutput(Json::encode($json));
    }   
}
?>

如何在OpenCart中处理文件上传以及如何将上传的文件插入数据库?怎么解决?请指导我......

0 个答案:

没有答案