[eluser]the_unforgiven[/eluser]
Im getting the error:
You did not select a file to upload.
But i did select a file to upload and have everything set up like so:
Controller
Code:
//Uploads
function _do_upload_file()
{
// Config
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = 'png|jpg|doc|docx|pdf|rtf|txt';
$config['max_size'] = '3000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile'))
{
$this->form_validation->set_message('_do_upload_file', $this->upload->display_errors());
return FALSE;
}
else
{
$filedata = $this->upload->data();
$this->filename = $filedata['file_name'];
// print_r($filedata);
return TRUE;
}
}
function addnews()
{
if ($this->session->userdata('username')) {
$this->form_validation->set_rules('title', 'News Title', 'required');
$this->form_validation->set_rules('body', 'News Body', 'required');
$this->form_validation->set_rules('userfile', 'Your File', 'callback__do_upload_file'); //Callback for errors
if ($this->form_validation->run() == FALSE)
{
$data['main_content'] = 'addnews';
$data['title']= 'Add News Post';
$this->load->view('template', $data);
}
else
{
$this->admin_model->insertnews();
$data['message'] = $this->session->set_flashdata('message','News item successfully stored.');
redirect('admin/news', $data);
}
}
else {
echo '[removed]
alert("Sorry you MUST be logged in to view this page, please login!");
location = "../";
[removed]';
}
}
Model
Code:
function insertnews()
{
$data = array(
'title' => $this->input->post('title'),
'body' => $this->input->post('body'),
);
$image_data = $this->upload->data();
$data['image'] = $image_data['file_name'];
$this->db->insert('news', $data);
}
View
Code:
<h1>Add News Item</h1>
<?php echo validation_errors('<div class="error">', '</div>'); ?>
<?php
$attributes = array('class' => 'email', 'id' => 'loginform');
echo form_open('admin/addnews', $attributes);
$upload = array('name'=>'userfile','id'=>'userfile');
?>
<table>
<tr><td>News Title:</td><td><?php echo form_input('title'); ?></td></tr>
<tr><td>Body:</td><td><textarea name="body" id="description"></textarea><?php echo display_ckeditor($ckeditor1); ?></td></tr>
<tr><td>Add File:</td><td><?php echo form_upload($upload); ?></td></tr>
</table>
<?php echo form_submit('submit', 'Create News Item'); ?>
<?php echo form_close();?>