创建自定义函数以上传和插入数据

时间:2018-08-14 08:12:14

标签: php codeigniter codeigniter-3 codeigniter-upload

我是否有可能创建一个自定义函数进行上传?我有2个函数,分别是insert_content()和insert_officials。在该功能中,有一个用于上传图像的代码。由于它们具有相同的config代码,因此我该怎么办。

公共函数insert_content(){         $ data = array('success'=> false,'messages'=> array());

    $name        = array('post_title','post_category','post_description');
    $label       = array('Title','Category','Description');
    $verify      = 'trim|required';
    $this->validate($name[0], $label[0], $verify);
    $this->validate($name[1], $label[1], $verify);
    $this->validate($name[2], $label[2], $verify);
    $this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>');

    if($this->form_validation->run()) {
        $data['success'] = true;
    } else {
        foreach($_POST as $key => $value) {
            $data['messages'][$key] = form_error($key);
        }
        echo json_encode($data);
        exit;
    }

    $date = date('Y');
    $month = date('m');
    $upload_config = array (
       'upload_path'  => './assets/uploads/'.$date.'/'.$month, 'allowed_types' => 'jpg|png|jpeg',
       'max_size'     => '2048', 'max_width'    => '1536', 'max_height'   => '1024'
    );

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

    if(!is_dir($upload_config['upload_path'])) {
        mkdir($upload_config['upload_path'], 0777, TRUE);
    }

    if($this->upload->do_upload('post_image')) {    
        $data = array('upload_data' => $this->upload->data());
        $image = $data['upload_data']['file_name'];

        $data = array (
            $name[0] => $this->pass($name[0]),
            $name[1] => $this->pass($name[1]),
            $name[2] => $this->pass($name[2]),
            'post_image' => $image
        );
        $this->model->CreateContent($data);
    }
}

//Officials
public function insert_officials() {
    $data = array('success' => false, 'messages' => array());

    $name   = array('official_fname','official_mname','official_lname','official_nickname','official_contact','official_position','official_barangay','official_address');
    $label  = array('First Name','Middle Name','Last Name','Nickname','Contact','Position','Barangay','Address');
    $verify = 'trim|required';
    $number = 'required|regex_match[/^[0-9]{11}$/]';
    $this->validate($name[0], $label[0], $verify);
    $this->validate($name[1], $label[1], $verify);
    $this->validate($name[2], $label[2], $verify);
    $this->validate($name[3], $label[3], $verify);
    $this->validate($name[4], $label[4], $number);
    $this->validate($name[5], $label[5], $verify);
    $this->validate($name[6], $label[6], $verify);
    $this->validate($name[7], $label[7], $verify);
    $this->form_validation->set_error_delimiters('<p class="text-danger">', '</p>');

    if($this->form_validation->run()) {
        $data['success'] = true;
    } else {
        foreach($_POST as $key => $value) {
            $data['messages'][$key] = form_error($key);
        }
        echo json_encode($data);
        exit;
    }

    $date = date('Y');
    $month = date('m');
    $upload_config = array (
       'upload_path'  => './assets/uploads/'.$date.'/'.$month, 'allowed_types' => 'jpg|png|jpeg',
       'max_size'     => '2048', 'max_width'    => '2048', 'max_height'   => '1024'
    );

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

    if(!is_dir($upload_config['upload_path'])) {
        mkdir($upload_config['upload_path'], 0777, true);
    }

    if (!$this->upload->do_upload('official_image')) {    
        $data = array('error' => $this->upload->display_errors('<span class="ti-remove"></span> '));

    } else {
        $data = array('upload_data' => $this->upload->data());
        $image = $data['upload_data']['file_name'];

        $data = array (
            $name[0] => $this->pass($name[0]),
            $name[1] => $this->pass($name[1]),
            $name[2] => $this->pass($name[2]),
            $name[3] => $this->pass($name[3]),
            $name[4] => $this->pass($name[4]),
            $name[5] => $this->pass($name[5]),
            $name[6] => $this->pass($name[6]),
            $name[7] => $this->pass($name[7]),
            'official_image' => $image
        );

        $this->model->CreateOfficials($data);
    }
}

1 个答案:

答案 0 :(得分:0)

只需创建一个函数并将其放入模型之类的东西中

function upload($fieldname);
    $date = date('Y');
    $month = date('m');
    $upload_config = array (
       'upload_path'  => './assets/uploads/'.$date.'/'.$month, 'allowed_types' => 'jpg|png|jpeg',
       'max_size'     => '2048', 'max_width'    => '1536', 'max_height'   => '1024'
    );

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

    if(!is_dir($upload_config['upload_path'])) {
        mkdir($upload_config['upload_path'], 0777, TRUE);
    }

    if($this->upload->do_upload($fieldname)) {
        return $this->upload->data();
    }  
    return false;
}

用法:

$this->load->model('somemodel');
$imageup = $this->somemodel->upload('post_image');
if ($imageup) {
    // image uploaded succesfully
    echo $imageup['file_name'];
} else {
    // upload failed
    // you should be able to also access error
    echo $this->upload->display_errors();
}