2
public function add_software()
{   
    $this->load->library('form_validation');

    $this->form_validation->set_error_delimiters('<div class="error-form">',   
'</div>');
    $this->form_validation->set_rules('name', 'Name', 
'required|min_length[4]|max_length[25]');
    $this->form_validation->set_rules('desc', 'Description', 'required');
    $this->form_validation->set_rules('licence', 'Licence.', 'required');
    $this->form_validation->set_rules('platform', 'Platform', 'required');
    $this->form_validation->set_rules('developers', 'Developer',     
'required');
    $this->form_validation->set_rules('link', 'Developer Website', 
'required');
    $this->form_validation->set_rules('cat', 'Category', 'required');
    $this->form_validation->set_rules('logo', 'Software Logo', 'required');

    if ($this->form_validation->run() == FALSE) {
    $this->load->view('admin/includes/header');
    $this->load->view('admin/add_software');
    $this->load->view('admin/includes/footer');
    } else {
    //Setting values for tabel columns
    $data['name']        = $this->input->post('name');
    $data['description'] = $this->input->post('desc');
    $data['licence']     = $this->input->post('licence');
    $data['platform']    = $this->input->post('platform');
    $data['developers']  = $this->input->post('developers');
    $data['link']        = $this->input->post('link');
    $data['category']    = $this->input->post('cat');
    $data['logo']    = $this->input->post('logo');

    /////////////////////////////////
    //set preferences

    $config = array(
    'upload_path' => '' .base_url(). '/assets/img/logo/',
    'allowed_types' => "gif|jpg|png|jpeg|pdf",
    'overwrite' => TRUE,
    'max_size' => "4096000", 
    'max_height' => "1024",
    'max_width' => "1024"
    );
            //load upload class library

    $this->load->library('upload', $config);
           $this->upload->data();
           $this->upload->do_upload();
    /////////////////////////////////
    //Transfering data to Model
    $this->insert_model->form_insert($data);
    $data['message'] = 'Data Inserted Successfully';
    //Loading View
    redirect('admin/view_softwares', 'refresh');
    }
}

This is my codeigniter controller function. Using this function, I can add data to the database. But, I am unable to upload image. Even upload_files is enabled in php.ini. I tried resolving this problem all day long, but all in vain. I have not used enctype in my html form because when I use enctype, I get the below error:

undefined Index filename
2
  • try 'upload_path' => './assets/img/logo/', Commented Feb 13, 2016 at 11:29
  • You have to use enctype="multipart/form-data" for uploading a file and Use $image_info = $this->upload->do_upload("name_of_input_type_file"); Commented Feb 13, 2016 at 12:18

2 Answers 2

1

Friend i wil give code that i have done for uploading image.This surely work. You can refer this.Any doubt,please ask,i wil help you

public function uploadImage() {
        $this->load->helper(array('form', 'url'));  
        $config['upload_path'] = 'assets/images/b2bcategory';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '1000';
        $config['max_width'] = '2024';
        $config['max_height'] = '1768';
        $config['width'] = 75;
        $config['height'] = 50;
        if (isset($_FILES['catimage']['name'])) {
            $filename = "-" . $_FILES['catimage']['name'];
            $config['file_name'] = substr(md5(time()), 0, 28) . $filename;
        }
        $config['overwrite'] = TRUE;
        $config['remove_spaces'] = TRUE;
        $field_name = "catimage";
        $this->load->library('upload', $config);
        if ($this->input->post('selsub')) {
            if (!$this->upload->do_upload('catimage')) {
                //no file uploaded or failed upload
                $error = array('error' => $this->upload->display_errors());
            } else {
                $dat = array('upload_data' => $this->upload->data());
                $this->resize($dat['upload_data']['full_path'],           $dat['upload_data']['file_name']);
            }
            $ip = $_SERVER['REMOTE_ADDR'];
            if (empty($dat['upload_data']['file_name'])) {
                $catimage = '';
            } else {
                $catimage = $dat['upload_data']['file_name'];
            }
            $data = array(            
                'ctg_image' => $catimage,
                'ctg_dated' => time()
            );
            $this->b2bcategory_model->form_insert($data);

        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Welcome dear.Always happy to help :)
0

Your path is wrong.

'upload_path' => '' .base_url(). '/assets/img/logo/',

Note: Make sure your upload path "assets folder" is in the main directory out side of application folder. You also may need to set folder permissions.

Try

'upload_path' => FCPATH . 'assets/img/logo/' 

or just

'upload_path' => './assets/img/logo/' 

To access file info after upload success preferences

$image_info = $this->upload->data();

echo $image_info['file_name'];

Or Through Model

$this->insert_model->form_insert($data,  $image_info = array());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.