[eluser]matt2012[/eluser]
Having spent a bit of time on this and seeing others here doing the same thought id post my controller here, which will:
1) validate form
2) validate upload
3) upload image
4) create thumbnail *edit changed to GD2 for better quality thumbnails
5) insert data into table
you will need to load validation and form library somewhere first.
There's my stuff in there which can be ignored (i.e. basic page set up).
Code:
function edit_service_6()
{
//basic page setup
$data = template($this->data['options']);
$pid = $this->input->post('id_dir1');
$id = $this->uri->segment(3,$pid);
$data['id'] = $id;
$data['content'] = 'admin/edit_service_6';
//upload set up
$data['error'] = '';
$data['upload_data'] = '';
//check if folder exists
if (!is_dir("./services/".$data['name'])){
//if not create it with correct permissions
mkdir ("./services/".$data['name'], 0777); }
$config['upload_path'] = './services/'.$data['name'];
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '500';
$config['max_width'] = '1024';
$config['max_height'] = '768';
//form set up
$rules['title'] = "required|min_length[5]";
$fields['title'] = 'title';
$this->validation->set_rules($rules);
$this->validation->set_fields($fields);
//thumbnail set up
$config2['image_library'] = 'GD2';
$config2['create_thumb'] = TRUE;
$config2['maintain_ratio'] = TRUE;
$config2['width'] = 300;
$config2['height'] = 200;
// check whether form has been run and validates
if ($this->validation->run() == FALSE)
{
//if no do nothing
}
//else yes so do upload
else
{
$this->load->library('upload', $config);
//if upload not success display errors
if ( ! $this->upload->do_upload())
{
$data['error'] = $this->upload->display_errors();
}
//else do form insert, image upload and image resize!!
else
{
//upload data
$data['upload_data'] = $this->upload->data();
//resize image
//needed this from upload data
$config2['source_image'] = $data['upload_data']['full_path'];
$this->load->library('image_lib', $config2);
$this->image_lib->resize();
//form insert
$type = str_replace('image/','.',$data['upload_data']['file_type']);
$this->Servicemodel->insert_photo($data['upload_data']['raw_name'], $data['upload_data']['file_name'], $type);
}
}
$data['images'] = $this->Servicemodel->get_service_photos($id);
$this->load->view('MyViews/container2', $data);
}