The problem im having is with the edit section in my code, i got the add article section working correctly, specifically the problems im having are with the image update,
when i create a new article and select a featured image it adds it and store the filename in the database,
the problem is when a new article was created and a featured image was not selected,
whenever i add an image in "edit" i always get "
You did not select a file to upload." error, all i want is to get the filename of the image but it always fails when its on
"edit" even though the same code is repeated on
"add".
I dont know what else to do to be able to get that filename, i looked around and still the get the same error, here is the complete add and edit method in the Articles Controller :
http://pastebin.com/K5juQea5
Edit View:
Code:
<form method="post" action="<?php echo base_url(); ?>admin/articles/edit/<?php echo $article->id; ?>">
<!-- other input fields here ... -->
<div class="form-group">
<label>Featured Image</label>
<?php if( $article->post_image == null || $article->post_image == '' ) :?>
<p><strong>No Image</strong></p>
<label>Insert Featured Image</label>
<input type="file" name="post_image" />
<?php else : ?>
<img src="<?php echo base_url() . 'uploads/' . $article->post_image ; ?>">
<?php endif; ?>
</div>
</form>
Articles.php Controller:
PHP Code:
/*=================================================================================
EDIT ARTICLE
==================================================================================*/
public function edit($id){
//Validation Rules
$this->form_validation->set_rules('title','Title');
$this->form_validation->set_rules('body','Body','trim|required');
$this->form_validation->set_rules('is_published','Publish','required');
$this->form_validation->set_rules('category','Category','required');
$data['categories'] = $this->Article_model->get_categories();
$data['users'] = $this->User_model->get_users();
$data['article'] = $this->Article_model->get_article($id);
$data['groups'] = $this->User_model->get_groups();
if ($this->form_validation->run() == FALSE){
//Views
$data['main_content'] = 'admin/articles/edit';
$this->load->view('admin/layouts/main', $data);
}
else {
// POST FEATURED IMAGE DATA
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1300';
$config['max_width'] = '1300';
$config['max_height'] = '1300';
$this->load->library('upload', $config);
echo 'after load library upload';
if( ! $this->upload->do_upload('post_image') ) {
echo $this->upload->display_errors() ;
echo '<br><h1>IMAGE COULD NOT BE UPLOADED</h1><br>';
$image = '';
}
else { // this is where it fails, it can never get here even if the image is selected
$this->upload->do_upload();
$data_upload_files = array('upload_data' => $this->upload->data() );
$image_data = $this->upload->data() ;
echo 'Data Upload Files: ' . print_r($data_upload_files) . ' <br>' ;
echo '<h1>IMAGE WAS SUCCESSFULLY UPLOADED CHECK DATABASE</h1>';
$image = $image_data['file_name'];
}
//Create Articles Data Array
$data = array(
'title' => $this->input->post('title'),
'body' => $this->input->post('body'),
'slug' => $this->input->post('slug'),
'post_image' => $image,
'category_id' => $this->input->post('category'),
'user_id' => $this->input->post('user'),
'access' => $this->input->post('access'),
'is_published' => $this->input->post('is_published'),
'in_menu' => $this->input->post('in_menu'),
'order' => $this->input->post('order')
);
//Articles Table Insert
$this->Article_model->update($data, $id);
//Create Message
$this->session->set_flashdata('article_saved', 'Your article has been saved');
//Redirect to pages
redirect('admin/articles');
}
}