Search code examples
phpcodeigniterfile-upload

Uploading image on codeigniter , shows error :The upload path does not appear to be valid


I am using php codeigniter Framework, and trying to upload image in the folder & shows in the profile view but here is what i get when i upload the file :-

 Array (
    [upload_path] => ./community/photos/
    [allowed_types] => jpeg|jpg|png
    [max_size] => 100
    [max_width] => 1024
    [max_height] => 768
    [overwrite] => 1
    [file_name] => 1600052487_Musical-Birthday-Candle-Magic-Lotus-Flower-Candles-Blossom-Rotating-Spin-Party-Candle-14Small-Candles-2layers-Cake.jpg
 ) 

The upload path does not appear to be valid.

profile.php controller

public function do_upload()
    {
        echo "<pre>";
        //print_r($_FILES);
        //die;
        if(!empty($_FILES['filename']['name'])){ 
            $config['upload_path']          = FCPATH.'application/';
            $config['allowed_types']        = 'jpeg|jpg|png';
            $config['max_size']             = 100;
            $config['max_width']            = 1024;
            $config['max_height']           = 768;
            $config['overwrite']            = true;
            $config['file_name']            = time().'_'.$_FILES['filename']['name'];

            print_r($config);
                        //echo var_dump(is_dir(FCPATH.'uploads/'));

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

            if ( ! $this->upload->do_upload('filename')) 
            {
                //echo "sdfs";
                print_r($this->upload->display_errors());
                die;
                   // $error = array('error' => $this->upload->display_errors());
                $this->session->set_flashdata ('success','Invalid Credentils');
                redirect('myProfile');
                 //   $this->load->view('admin/profile/index', $error);
            }
            else
            {
                echo "xcvcx";
                die;
                    $data = array('user_profile' => $this->upload->data());

                    $this->load->view('admin/profile/index', $data);
            }
        }else{
            echo "dfgfd";
        }
    }

index.php view

<h2 class="heading">Profile</h2>
         <div class="row">
           <div class="col-md-3">
            <div class="profile-img">
                 <?php if (!empty($user->photo)): ?>
               <img src="/uploads/<?php echo $user->photo; ?>" alt="<?php echo $user->photo; ?>">
                  <?php endif;?>
                  <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcS52y5aInsxSm31CvHOFHWujqUx_wWTS9iM6s7BAm21oEN_RiGoog" alt=""/>
                  
                 
                 <?php echo form_open_multipart('admin/profile/do_upload');?>

                 <div class="file btn btn-lg btn-primary">
                     <i class="fa fa-pencil-square-o"></i>
                      <input id="choose_image" type="file" name="filename" />
                </div>
                <button id="SubmitBtn" type="submit" style="display:none">Upload photo</button>
                </form>
            </div>

autoload.php

$autoload['libraries'] = array('database','session','form_validation','encryption','upload');
$autoload['helper'] = array('url','form', 'file');

I have tried several solution but still getting the same error .


Solution

  • Have you try to put initializes the CI file upload class before doing do_upload? .

    i usully use this one

     $this->upload->initialize($set_upload);//my func class for config
       //generally they use $this->upload->initialize()
            if (!$this->upload->do_upload('userfile')){
    
    
            }