0

是否有机会创建自定义上传功能?我有 2 个函数,即 insert_content() 和 insert_officials。在该函数中有一个用于上传图像的代码。我将如何做到这一点,因为他们有相同的配置代码。

public function 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);
    }
}
4

1 回答 1

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();
}
于 2018-08-14T09:33:43.597 回答