0

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?

2
  • You need to give absolute path to upload file Commented Mar 17, 2018 at 4:40
  • how do i do that please help Commented Mar 17, 2018 at 4:41

4 Answers 4

5

please update this line,

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

to,     $path= FCPATH.'assets/front/img';
Sign up to request clarification or add additional context in comments.

Comments

2

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');
            }

Comments

1

You can use FCPATH this :

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

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

Comments

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'); 

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.