Ibrahim Alsaman Ibrahim Alsaman - 6 months ago 14
PHP Question

codeigniter upload file with another fields

I have in my application view like :



<div class="form-group">
<label class="col-md-4 control-label" for="content_textarea">Content</label>
<div class="col-md-4">
<textarea class="form-control" id="content_textarea" name="ad_news_content"></textarea>
</div>
</div>
<!-- Select Basic -->
<div class="form-group">
<label class="col-md-4 control-label" for="news_category">Category</label>
<div class="col-md-4">
<select id="news_category" name="ad_news_category" class="form-control">
<?php
foreach ($news_category_data as $ncat ) {?>
<option
value="<?php echo $ncat->nc_id ?>"><?php echo $ncat->nc_id ," - ", $ncat->news_category_name ?>
</option>
<?php } ?>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="news_create_date">Date</label>
<div class=" col-md-5">
<input type="text" placeholder="click to show datepicker" id="datepic">
</div>
</div>
<!-- main image upload -->
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="news_upload_img">Main Image</label>
<div class=" col-md-5">
<input type="file" name="userfile" size="20" />

</div>
</div>
<!-- end of main image upload -->


<!-- Button (Double) -->

<div class="form-group">
<label class="col-md-4 control-label" for="edit_btn"></label>
<div class="col-md-8">
<button id="edit_btn" name="add_btn" class="btn btn-primary"
formaction="<?php echo base_url() ."admin/news/insertNews"?>">Update</button>





and in my controller I have function to insert into data base and upload image

public function insertNews()
{
$config = array(
'upload_path' => './uploads/up_news',
'allowed_types' => 'gif|jpg|png',
'max_size' => '100',
'max_width' => '1024',
'max_height' => '768',
'encrypt_name' => true,
);

$this->load->library('upload', $config);
$this->upload->initialize($config);
$upload_data = $this->upload->data();
$data_ary = array(
'title' => $upload_data['client_name'],
'file' => $upload_data['file_name'],
'width' => $upload_data['image_width'],
'height' => $upload_data['image_height'],
'type' => $upload_data['image_type'],
'size' => $upload_data['file_size'],
'path' => $upload_data['full_path'],
'date' => time(),
);
$data = array('upload_data' => $upload_data);
$this->load->model('newsModel');
$ad_ne_data = array(
'titel' => $this->input->post('ad_news_title') ,
'content' => $this->input->post('ad_news_content') ,
'news_category_id' => $this->input->post('ad_news_category') ,
'img_url' => $data_ary['path']."".$data_ary['file'],
'created_at' => date("Y-m-d")
);

$this->newsModel->addNews($ad_ne_data);
}


the code is run ok but the image not uploaded and in the database is insert just upload path without image name not full path
so can you correct my wrong or give me example to upload image with other fields but every image have spectate folder like news in uploaded_news, posts in uploaded_post

Answer

When you need to upload image you need to use do_upload() I would recommend using form_helper form_open_multipart

 $config = array(
            'upload_path'   => './uploads/up_news',
            'allowed_types' => 'gif|jpg|png',
            'max_size'      => '100',
            'max_width'     => '1024',
            'max_height'    => '768',
            'encrypt_name'  => true,
 );

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

if ($this->upload->do_upload('userfile')) {

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

$data_ary = array(
'title'     => $upload_data['client_name'],
'file'      => $upload_data['file_name'],
'width'     => $upload_data['image_width'],
'height'    => $upload_data['image_height'],
'type'      => $upload_data['image_type'],
'size'      => $upload_data['file_size'],
'path'          => $upload_data['full_path'],
'date'      => time(),
);

$data = array('upload_data' => $upload_data);

$this->load->model('newsModel');

$ad_ne_data = array(
'titel' => $this->input->post('ad_news_title') ,
'content' => $this->input->post('ad_news_content') ,
'news_category_id' => $this->input->post('ad_news_category') ,
'img_url' => $data_ary['path']."".$data_ary['file'],
'created_at' => date("Y-m-d")
);

$this->newsModel->addNews($ad_ne_data);

} else {

   // Some Error

}

Second Idea Using callback

Filename: Example.php

class Example extends CI_Controller {

public function __construct() {

    parent::__construct();

    $this->load->library('form_validation');
    $this->load->helper('form');

}

public function index() {

    $data['title'] = 'Ad';

    $this->form_validation->set_rules('ad_news_title', 'Ad Title', 'required');
    $this->form_validation->set_rules('ad_news_content', 'Content', 'callback_insertNews');

    if ($this->form_validation->run() == FALSE) {

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

    } else {

        redirect('success_controller');
    }

}

public function insertNews() {
     $config = array(
                'upload_path'   => './uploads/up_news',
                'allowed_types' => 'gif|jpg|png',
                'max_size'      => '100',
                'max_width'     => '1024',
                'max_height'    => '768',
                'encrypt_name'  => true,
     );

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

    if ($this->upload->do_upload('userfile')) {

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

    $data_ary = array(
    'title'     => $upload_data['client_name'],
    'file'      => $upload_data['file_name'],
    'width'     => $upload_data['image_width'],
    'height'    => $upload_data['image_height'],
    'type'      => $upload_data['image_type'],
    'size'      => $upload_data['file_size'],
    'path'          => $upload_data['full_path'],
    'date'      => time(),
    );

    $data = array('upload_data' => $data_ary);

    $this->load->model('newsModel');

    $ad_ne_data = array(
        'titel' => $this->input->post('ad_news_title') ,
        'content' => $this->input->post('ad_news_content') ,
        'news_category_id' => $this->input->post('ad_news_category') ,
        'img_url' => $data_ary['path']."".$data_ary['file'],
        'created_at' => date("Y-m-d")
    );

    $this->newsModel->addNews($ad_ne_data);

    } else {

       // Some Error

       $this->form_validation_set_message('insertNews', $this->upload->display_errors());

       return FALSE;

    }

}


}

View Form Open

<?php echo form_open_multipart('example/insertNews');?>

// The rest of the form content goes here

<?php echo form_close();?>

Make sure you have set you base Url in CI3