Created
July 22, 2013 05:39
-
-
Save WenLiangTseng/6051511 to your computer and use it in GitHub Desktop.
CodeIgniter中,同時上傳圖片與文字
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // controller | |
| function do_upload() | |
| { | |
| $config['upload_path'] = './uploads/'; | |
| $config['allowed_types'] = 'gif|jpg|png'; | |
| $config['max_size'] = '100'; | |
| $config['max_width'] = '1024'; | |
| $config['max_height'] = '768'; | |
| $this->load->library('upload', $config); | |
| // validate the POST data | |
| // http://codeigniter.com/user_guide/libraries/form_validation.html | |
| $this->form_validation->set_rules('title', 'Title', 'trim|required'); | |
| $this->form_validation->set_rules('description', 'Description', 'trim|required') | |
| if ($this->form_validation->run() == FALSE) | |
| { | |
| // failed validation | |
| $this->load->view('myform'); | |
| // quit here | |
| return false; | |
| } | |
| if ( ! $this->upload->do_upload()) | |
| { | |
| // no file uploaded or failed upload | |
| $error = array('error' => $this->upload->display_errors()); | |
| $this->load->view('upload_form', $error); | |
| } | |
| else | |
| { | |
| // success | |
| $data = array('upload_data' => $this->upload->data()); | |
| $title = $this->input->post('title'); | |
| $description = $this->input->post('description'); | |
| // a model that deals with your image data (you have to create this) | |
| $this->your_upload_model->add($title, $description, $data["file_name"]); | |
| $this->load->view('upload_success', $data); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // view | |
| echo form_open_multipart('controller_name/do_upload'); | |
| echo form_input("title", ""); | |
| echo form_input("description", ""); | |
| echo form_upload("userfile"); | |
| echo form_close; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment