Ant
Ant

Reputation: 21

picture upload in codeigniter

Everything is fine, but when I open the upload folder there is not any image.

here is the controller "admin_news". I try to upload the image file in localhost/site/asset/upload. But when i click submit only the information(title, news and category) goes into datebase, but the file is not uploaded.

    function __construct()
{
    parent::__construct();
    $this->load->helper(array('form', 'html', 'file'));
    $config['upload_path']   = base_url('asset/upload/');
    $config['allowed_types'] = 'gif|jpg|png';
    $this->load->library('upload', $config);
}


public function add_news(){
    $this->load->helper('form');  
    $this->load->model('AddNews');  
    $this->load->view('templates/header', $data);
    $this->load->view('admin/add_news');
    $this->load->view('templates/footer');
    if($this->input->post('submit')){
        if ($this->upload->do_upload('image'))
        {
            $this->upload->initialize($config);
            $this->upload->data();
        }
        $this->AddNews->entry_insert();
        redirect("admin_news/index");
    }
}

The view has only:

        <?php echo form_open_multipart(''); ?>
    <input type="text" name="title" value="Title..." onfocus="this.value=''" /><br /> 
    <input type="file" name="image" /><br />

....

Upvotes: 0

Views: 293

Answers (2)

newday
newday

Reputation: 3878

I think in your code, problem is you have declared configuration after do_upload method

if ($this->upload->do_upload('image'))
        {
            $this->upload->initialize($config);
            $this->upload->data();
        }

configuration should be initialized before use the method. I think that was the problem. so you have to do configuration before the do_upload method

Upvotes: 0

No Results Found
No Results Found

Reputation: 102854

This should not be a URL:

$config['upload_path']   = base_url('asset/upload/');

It should be a path to somewhere on your server, either a full absolute path or a relative one (all paths in Codeigniter are relative from index.php).

Use either one of these:

// Full path
$config['upload_path'] = FCPATH.'asset/upload/';

// Relative
$config['upload_path'] = './asset/upload/';

One more thing:

if ($this->upload->do_upload('image'))
{
    // You don't need this, and besides $config is undefined
    // $this->upload->initialize($config);

    // You don't seem to be doing anything with this?
    $this->upload->data();

    // Move this here in case upload fails
    $this->AddNews->entry_insert();
    redirect("admin_news/index");
}
// Make sure to show errors
else
{
    echo $this->upload->display_errors();
}

Upvotes: 1

Related Questions