1

if a image is choosed then i want to upload that image and store filename to db else i want to store in db filename as "default_brand.png" and no file is going to upload . in following do_upload function if errors occured then i want to display that error on same view, if no error and image uploaded successfully i want to display success message. what changes i have to do for following code.

function index()
  {
//some statements.
if (empty($_FILES['brand_image']['name'])) {
            $filename = 'default_brand.png';
        }
        else {
            $filename = $this->do_upload();
        }
        echo $filename;
}
    function do_upload()
    {
        $config['upload_path'] = './assets/public_image/brand';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';


    $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload('brand_image'))
        {
            $error = array('error' => $this->upload->display_errors());

        $this->load->model('get_data_model');
        $resultset = $this->get_data_model->get_only_child_categories();    
        $data['resultset'] = $resultset;
        $this->layouts->view('brand_view', array('left_sidebar' => 'sidebar/left_sidebar','right_sidebar' => 'sidebar/right_sidebar'),$data,true);
        return "default_brand.png";
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
            $filename = $data['file_name'];
            return $filename;
        }
    }
2
  • I'm sorting through this now. Have a question. Are you using a custom view library? Is that the reason you are calling $this->layouts->view instead of $this->load->view? Commented Apr 29, 2015 at 21:30
  • yeah. i have created a library for layout Commented Apr 30, 2015 at 8:16

1 Answer 1

2

I have modified your original code to use a single function. It will load the index page on initial page load and will only execute the upload process if a user is actually posting data to the page (uploading a file).

I set the same variables for use within the view. You can use them to display success message or error. (This code is also assuming you have a custom "layouts" library you are using to render your views which it looks like that's what is going on. If not then that line will need to be changed to use codeigniter's default view rendering process)

function index(){

$post = $this->input->post();
$this->load->library('upload', $config);
$this->load->model('get_data_model');

if(!$post){ //your initial page load. user not submitting file since not posting data

    $data = array(

        'error'     => FALSE,
        'file_name' => '',
        'resultset' =>  $this->get_data_model->get_only_child_categories()

    );

    $this->layouts->view('brand_view', array('left_sidebar' => 'sidebar/left_sidebar','right_sidebar' => 'sidebar/right_sidebar'),$data,true);

} else { //this is hit if user posting data (uploading file)

    $config['upload_path'] = './assets/public_image/brand';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

    $data = array(

        'error'     => $this->upload->do_upload('brand_image') ? FALSE : $this->upload->display_errors('',''),
        'file_name' => isset($upload_data['file_name']) ? $upload_data['file_name'] : 'default_brand.png',
        'resultset' =>  $this->get_data_model->get_only_child_categories()

    );

    $this->layouts->view('brand_view', array('left_sidebar' => 'sidebar/left_sidebar','right_sidebar' => 'sidebar/right_sidebar'),$data,true);

}

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

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.