如何将数组从控制器传递到codeigniter中的视图

时间:2014-05-01 17:25:32

标签: image codeigniter upload

我想在codeigniter中上传文件,并希望在视图文件中显示错误,但显示错误数组未显示在视图文件中。上传文件失败时,视图中不显示相关错误

function add_model()
{
    $this->checkuser();

    if(isset($_POST['add_model']))
    {
        $config['upload_path'] = 'uploads/model/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = '1000';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        $config['file_name']  = rand(1111, 99999).'_'.$_FILES['userfile']['name'];
        $config['overwrite']  = TRUE;
        $config['encrypt_name']  = TRUE;
        $config['remove_spaces']  = TRUE;

        $this->load->library('upload', $config);
        $this->upload->initialize($config);

        $mDate = date('Y-m-d H:i:s');

        $this->form_validation->set_rules('model_name', 'Model Name', 'trim|xss_clean|required');
        $this->form_validation->set_rules('model_slug', 'Model Slug', 'trim|xss_clean|required');

        if($this->form_validation->run() && $this->upload->do_upload())
        {
            $temp = $this->upload->data('userfile');
            $model_image = $temp['file_name'];

            $data = array(
                    'model_name' => $this->input->post('model_name'),
                    'model_image' => $model_image,
                    'brand_id' => $this->input->post('brand_id'),
                    'model_slug' => $this->toAscii($this->input->post('model_slug'))
                    );

            $this->model_model->addmodel($data);
            redirect('admin/model/model_listing');
        }
        else
        {
            $data['errors'] = array('error' => $this->upload->display_errors());
            $data['brands'] = $this->model_model->allbrand();
            $data['page_title'] = 'Add Content';
            $this->load->view('admin/add_model', $data);    
        }
    }
    else
    {
            $data['errors'] = $this->upload->display_errors();
            $data['brands'] = $this->model_model->allbrand();
            $data['page_title'] = 'Add Content';
            $this->load->view('admin/add_model', $data);        
    }


}

下面给出的是我希望显示错误的视图文件的代码

<?php

    if(validation_errors())
    {
        echo validation_errors('<div class="alert alert-error">', '</div>');
        echo '<div class="alert alert-error">'.$error.'</div>';
    }
    if($errors)
    {
         foreach ($errors as $error) 
         {
             echo '<div class="alert alert-error">'.$error.'</div>';
         }
    }
?>

我同时使用验证错误和显示错误

1 个答案:

答案 0 :(得分:0)

如果条件

,则更改此项
if($this->form_validation->run() && $this->upload->do_upload())
        {

到这个

if($this->form_validation->run() && $this->upload->do_upload('name'))
        {

do_upload函数需要字段名称才能上传图片..

相关问题