Codeigniter - How to add Images to my CRUD methods?
I am working on my second CI application. I have created some simple CRUD methods in my controller. My client has requested that each record includes an image. I have searched the forums and other resources for help but haven’t had much luck.
I have managed to upload files into a directory using the File Uploading Class, my problem though is how to associate the uploaded file/s with the relevant record.
Here are the relevant parts of my MVC.., any help/point in the right direction would be appreciated.
View - admin/locationEdit.php
<form method="post" action="<?php echo $action; ?>">
<div class="data">
<table>
<tr>
<td width="30%">ID</td>
<td><input type="text" name="id" disabled="disable" class="text" value="<?php echo $this->validation->id; ?>"/></td>
<input type="hidden" name="id" value="<?php echo $this->validation->id; ?>"/>
</tr>
<tr>
<td valign="top">Name<span style="color:red;">*</span></td>
<td><input type="text" name="name" class="text" value="<?php echo $this->validation->name; ?>"/>
<?php echo $this->validation->name_error; ?></td>
</tr>
<tr>
<td valign="top">Address<span style="color:red;">*</span></td>
<td><input type="text" name="address" class="text" value="<?php echo $this->validation->address; ?>"/>
<?php echo $this->validation->address_error; ?></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Save"/></td>
</tr>
</table>
</div>
</form>
Controller - location.php
function add(){
// set validation properties
$this->_set_fields();
// set common properties
$data['title'] = 'Add new location';
$data['message'] = '';
$data['action'] = site_url('admin/location/addLocation');
$data['link_back'] = anchor('admin/location/index/','Back to list of locations',array('class'=>'back'));
// Write to $title
$this->template->write('title', 'Admin - Add New Location!');
// Write to $sidebar
$this->template->write_view('content', 'admin/locationEdit', $data);
// Render the template
$this->template->render();
}
function addLocation(){
// set common properties
$data['title'] = 'Add new location';
$data['action'] = site_url('admin/location/addLocation');
$data['link_back'] = anchor('admin/location/index/','Back to list of locations',array('class'=>'back'));
// set validation properties
$this->_set_fields();
$this->_set_rules();
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$path_to_uploads='./uploads';
$config['upload_path'] = $path_to_uploads;
$this->load->library('upload', $config);
$upload_data=$this->upload->data();
$file_name=$upload_data['file_name'];
$full_file_path = $path_to_uploads.'/'.$file_name;
// run validation
if ($this->validation->run() == FALSE){
$data['message'] = '';
}else{
// save data
$location = array('name' => $this->input->post('name'),
'address' =>开发者_运维百科 $this->input->post('address'),
'image_url' => $full_file_path);
$id = $this->locationModel->save($location);
// set form input name="id"
$this->validation->id = $id;
// set user message
$data['message'] = '<div class="success">add new location success</div>';
}
// Write to $title
$this->template->write('title', 'Admin - Add New Location!');
// Write to $sidebar
$this->template->write_view('content', 'admin/locationEdit', $data);
// Render the template
$this->template->render();
}
Within your upload function, get the path of the uploaded file:
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$path_to_uploads='./uploads';
$config['upload_path'] = $path_to_uploads;
$this->load->library('upload', $config);
//add this
$this->upload->initialize($config);
if (!$this->upload->do_upload()){
$error = $this->upload->display_errors();
echo "<script>alert($error);</script>";
}else{
$upload_data=$this->upload->data();
$file_name=$upload_data['file_name'];
$full_file_path = $path_to_uploads.'/'.$file_name;
}
then you can return $full_file_path
back to the method that called it and insert it into the db, or just insert directly.
精彩评论