Sikander
Sikander

Reputation: 2835

Codeigniter : upload path does not appear to be valid

I am using the following function in my model:

function uploadsinglepicture($uploadpath){

    $config['upload_path'] =$uploadpath;
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '';
    $config['max_width']  = '';
    $config['max_height']  = '';
    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload('userfile'))
    {
        $error = array('error' => $this->upload->display_errors());
        print_r($error);
        if($this->input->post('id') == ''){
            $insertion['image'] = '';
        }
        //$this->load->view('upload_form', $error);
    }
    else{
        $data = array('upload_data' => $this->upload->data());
        $insertion['image'] = $data['upload_data']['file_name'];
    }

    $image = $insertion['image'];
    return $image;
}

This is how I access the function in controller:

if(!empty($this->input->post())){
                $path= base_url().'assets/front/img';
                $this->general->uploadsinglepicture($path);
                redirect(base_url().'admin/home/index/sliderupated');
}

but the error I get is:

Array ( [error] => The upload path does not appear to be valid. )

If I print $path, this is what I get

http://localhost/site/assets/front/img/

and that opens in the browser as real path. My code in view is as below

<form  method="post" enctype="multipart/form-data" action ="<?=base_url()?>admin/home/index" >
    <label>Upload Picture </label>
    <input type='file'  name='userfile' />
    <input type="hidden" name="updateimage">
    <input type="submit" class="btn btn-primary pull-right" />
</form>

How can I fix the error?

Upvotes: 0

Views: 1340

Answers (4)

Abhijit Maji
Abhijit Maji

Reputation: 1

FCPATH: front controller path where index.php exists or root folder

APPPATH: application folder

Create a uploads folder under root folder:

$config['upload_path'] = realpath(FCPATH.'uploads');

Create a uploads folder under application folder:

$config['upload_path'] = realpath(APPPATH.'uploads');

In case: If you have created the uploads folder outside of root folder:

$config['upload_path'] = realpath($_SERVER['DOCUMENT_ROOT'].'/uploads'); 

Upvotes: 0

B. Desai
B. Desai

Reputation: 16436

Instead of giving base_url() you need to give base directory. So change your code to:

if(!empty($this->input->post())){
                $path= FCPATH.'assets/front/img';
                $this->general->uploadsinglepicture($path);
                redirect(base_url().'admin/home/index/sliderupated');
            }

Upvotes: 2

Pradeep
Pradeep

Reputation: 9717

You can use FCPATH this :

  $path= FCPATH.'assets/front/img';

for more : https://www.codeigniter.com/user_guide/general/reserved_names.html

Upvotes: 1

Mayank Majithia
Mayank Majithia

Reputation: 1966

please update this line,

from ,  $path= base_url().'assets/front/img'; 

to,     $path= FCPATH.'assets/front/img';

Upvotes: 5

Related Questions