1

これは私のコントローラーです

  function new_post() {
    if ($_POST) {

        $config = array( array('field' => 'title', 'rules' => 'trim|requird'),
                        array('field' => 'post', 'rules' => 'trim|required'));
        $this -> load -> library('form_validation');
        $this -> form_validation -> set_rules($config);
        if ($this -> form_validation -> run() == false) {
            $data['errors'] = validation_errors();
        }
        if ($this->input->post('upload')) {
        $this->Gallery_model->do_upload();
        }
        $data = array('title' => $_POST['title'],
                       'post' => $_POST['post'],
                       'active' => $_POST['active']);
        $this -> post_m -> insert_post($data);
        redirect(base_url() . 'posts/index_admin');
    } else {
        $this -> load -> view('admin/post_new', $data);
    }

}

これはモデルコード クラス Gallery_model extends Model {

var $gallery_path;
var $gallery_path_url;

function Gallery_model() {
    parent::Model();
    $this->gallery_path = realpath(APPPATH . '../images');
    $this->gallery_path_url = base_url().'images/';
}

function do_upload() {

    $config = array(
        'allowed_types' => 'jpg|jpeg|gif|png',
        'upload_path' => $this->gallery_path,
        'max_size' => 2000
    );

    $this->load->library('upload', $config);
    $this->upload->do_upload();
    $image_data = $this->upload->data();

    $config = array(
        'source_image' => $image_data['full_path'],
        'new_image' => $this->gallery_path . '/thumbs',
        'maintain_ration' => true,
        'width' => 150,
        'height' => 100
    );

    $this->load->library('image_lib', $config);
    $this->image_lib->resize();

}

これはビューコードです

form_open_multipart('posts/new_post');
             form_textarea('title');
             form_textarea('post');
             form_input('active');
             form_upload('userfile');
             form_submit('upload', 'Upload');
             form_close();

私のテーブルは現在、正常にアップロードされていますが、{fileurl} という名前で追加した新しい列にファイル名をアップロードしたいだけです。

4

1 に答える 1

0

do_upload関数でモデルから画像名を返す

function do_upload() {

$config = array(
    'allowed_types' => 'jpg|jpeg|gif|png',
    'upload_path' => $this->gallery_path,
    'max_size' => 2000
);

$this->load->library('upload', $config);
$this->upload->do_upload();
$image_data = $this->upload->data();
//get the image name
$fileurl = $image_data['file_name'];

$config = array(
    'source_image' => $image_data['full_path'],
    'new_image' => $this->gallery_path . '/thumbs',
    'maintain_ration' => true,
    'width' => 150,
    'height' => 100
);

$this->load->library('image_lib', $config);
$this->image_lib->resize();

return $fileurl;

}

そして、コントローラーは次のようになります--

function new_post() {
if ($_POST) {

    $config = array( array('field' => 'title', 'rules' => 'trim|requird'),
                    array('field' => 'post', 'rules' => 'trim|required'));
    $this -> load -> library('form_validation');
    $this -> form_validation -> set_rules($config);
    if ($this -> form_validation -> run() == false) {
        $data['errors'] = validation_errors();
    }
    if ($this->input->post('upload')) {
    $fileurl = $this->Gallery_model->do_upload();
    }
    $data = array('title' => $_POST['title'],
                   'post' => $_POST['post'],
                   'fileurl' => $fileurl,
                   'active' => $_POST['active']);

    $this -> post_m -> insert_post($data);
    redirect(base_url() . 'posts/index_admin');
} else {
    $this -> load -> view('admin/post_new', $data);
}

}

これがうまくいくことを願って試してみてください。

于 2013-05-26T12:22:15.947 に答える