1

This is my controller file that receives input from view file. post data will be submited to Add function and the function manipulates do upload function which recieves configuration parameters from set_configuration_function.

public function __construct()
{
    parent::__construct();
    $this->load->model('App/AppModel');//load app model
    $this->load->helper('url');
    $this->load->helper('form');
    $this->load->library('session');
}

public function index()
{
    $this->data['blogs'] = $this->AppModel->get_all(BLOGS_TABLE);
    parent::LOADER('App/Blog/All');
}

public function Add()
{


    if ($_POST && empty($_FILES['blog_file']['name'])) {
        $new_blog = $_POST;
        $this->AppModel->insert(BLOGS_TABLE, $new_blog);
        $this->session->set_flashdata('flashSuccess', 'Blog Created Successfully.');
        $this->index();
    }

    if ($_POST && !empty($_FILES['blog_file']['name'])) {
        $file_name =$this->do_upload();
        $file_name = array($file_name);

        $blog = $_POST;
        $new_blog = array_merge($blog, $file_name);
        $this->AppModel->insert(BLOGS_TABLE, $new_blog);
        $this->session->set_flashdata('flashSuccess', 'Blog Created Successfully.');
        $this->index();
    }
    parent::LOADER('App/Blog/New');
}


function do_upload()
{
    $config['upload_path'] = './uploads/';
    $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())
    {
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('upload_form', $error);
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());

        $this->load->view('upload_success', $data);
    }
}


public function set_upload_options (){

    $config = array();
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['max_size'] = '10000000000';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';
    return $config;
}

This is my View File

<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header col-md-offset-1">
        <h1>
          <?php echo BLOG_HEADING; ?>
            <small><?php echo ADD; ?></small>
        </h1>

    </section>

    <!-- Main content -->
    <section class="content">


        <div class="row">


            <!-- right column -->
            <div class="col-md-8 col-md-offset-1">

                <!-- general form elements disabled -->
                <div class="box box-warning">
                    <div class="box-header with-border">
                        <h3 class="box-title"><?php echo ADD; ?> &nbsp;Blog</h3>
                    </div>
                    <!-- /.box-header -->
                    <?php echo form_open_multipart('App/Blog/Add');?>
                    <div class="box-body">

                            <!-- text input -->
                            <!-- input states -->
                            <div class="form-group has-success">
                                <label class="control-label" for="inputSuccess"> <?php echo BLOG_TITLE; ?> *</label>
                                <input type="text" name="blog_title" class="form-control" id="inputSuccess" placeholder="Enter Title" required>

                            </div>
                        <div class="form-group has-success">
                            <label class="control-label" for="inputSuccess"> <?php echo BLOG_AUTHOR; ?> *</label>
                            <input type="text" name="blog_author" class="form-control" id="inputSuccess" placeholder="Enter Author" required>

                        </div>

                        <div class="form-group has-success">
                            <label class="control-label" for="inputSuccess"> <?php echo BLOG_URL; ?></label>
                            <input type="text" name="blog_url" class="form-control" id="inputSuccess" placeholder="Add Link" required>

                        </div>


                            <!-- textarea -->
                            <div class="form-group has-success">
                                <label class="control-label"  for="inputSuccess"><?php echo BLOG_CONTENT; ?> *</label>
                                <textarea name="blog_content" class="form-control" rows="3" placeholder="Enter Content" required></textarea>
                            </div>
                            <div class="form-group has-success">
                                <label for="exampleInputFile">Blog Image</label>
                                <input type="file" name="blog_file" id="exampleInputFile">
                            </div>

                    </div>
                    <!-- /.box-body -->

                    <div class="box-footer">
                        <button type="submit" class="btn btn-primary"><?php echo SAVE; ?></button>
                    </div>
                    </form>
                    </div>
            </div>

    </section>
</div>

Problem is I get error that says "you did not select a file to upload."

1 Answer 1

1
  1. you are not using set_upload_options() method.

  2. you are missing input name blog_file in your controller.

$this->upload->do_upload('blog_file');

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.