简体   繁体   中英

Can't store the uploaded image path in database using codeigniter

I read similar query on google, there I read about checking whether file is writable and then setting permissions using chmod() function, but I tried that too, it didnt work. I want to store the image path in database, and move the image to the uploads folder. The path of the image would be as : C:/xampp/htdocs/konnect1/uploads/Hydrangeas1.jpg On using chmod(), I get Warning as "Message: chmod(): No such file or directory". please help as what should I change now.

Controller page->admin_c.php Posting the function, where image upload code is written.

          public function create_event1()
         {
        if($this->input->post('counter') || !$this->input->post('counter'))
        {

            $count = $this->input->post('counter');
            $c = $count;
            //echo $c;          


            if($this->input->is_ajax_request())
            {
                $vardata    =   $this->input->post('vardata');
                echo $vardata;

            } 

            $g = $_POST['results'];

            $configUpload['upload_path']    = '/konnect1/uploads/';                 #the folder placed in the root of project
            $configUpload['allowed_types']  = 'gif|jpg|png|bmp|jpeg';       #allowed types description
            $configUpload['max_size']       = '0';                          #max size
            $configUpload['max_width']      = '0';                          #max width
            $configUpload['max_height']     = '0';                          #max height
            $configUpload['encrypt_name']   = false;                         #encrypt name of the uploaded file

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

            $this->upload->initialize($configUpload);   #init the upload class

            if( chmod($configUpload['upload_path'], 0755) ) 
            {
                // more code
                chmod($configUpload['upload_path'], 0777);
            }
            else
                echo "Couldn't do it."; 


            if ( ! is_writable($this->upload->do_upload('picture')))
            {
                $uploadedDetails = $this->upload->display_errors('upload_not_writable');
                echo $uploadedDetails;
            }
            else if(!$this->upload->do_upload('picture'))
            {
                $uploadedDetails    = $this->upload->display_errors();
            }
            else
            {

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

            //print_r($uploadedDetails);die;

            $etype = $this->input->post('etype');
            $ecategory = $this->input->post('ecategory');
            $ename = $this->input->post('ename');

            $edat_time = $this->input->post('edat_time');
            $evenue = $this->input->post('evenue');
            $sch_name0 = $this->input->post("sch_name0");
            $speaker_name0 = $this->input->post("speaker_name0");
            $sch_stime0 = $this->input->post("sch_stime0");
            $sch_etime0 = $this->input->post("sch_etime0");
            $sch_venue0 = $this->input->post("sch_venue0");
            $sch_name = $this->input->post("sch_name");
            $speaker_name = $this->input->post("speaker_name");
            $sch_stime = $this->input->post("sch_stime");
            $sch_etime = $this->input->post("sch_etime");
            $sch_venue = $this->input->post("sch_venue");
            $agenda_desc = $this->input->post("agenda_desc");

            if ((!empty($etype)) || (!empty($uploadedDetails)) || (!empty($ecategory)) || (!empty($ename)) || (!empty($edat_time)) || (!empty($evenue)) || (!empty($sch_name0)) || (!empty($speaker_name0)) || (!empty($sch_stime0)) || (!empty($sch_etime0)) || (!empty($sch_venue0)) || (!empty($sch_name)) || (!empty($speaker_name)) || (!empty($sch_stime)) || (!empty($sch_etime)) || (!empty($sch_venue)) || (!empty($agenda_desc)))
            {

                $res1 = $this->admin_m->insert($uploadedDetails);

                if($res1 == true)
                {
                    $res2 = $this->admin_m->insert1($c);

                    $lastid = $this->db->insert_id(); 

                    $data['h'] = $this->admin_m->select($lastid); 

                    //return the data in view   
                    $this->load->view('admin/event', $data);
                }
                else
                    echo "error";



            }
           }
        }   


      }

Model Page->admin_m.php

<?php 

   class Admin_m extends CI_Model 
   {

      function __construct() 
      { 
        parent::__construct();
        $this->load->database();        
      } 


      public function insert($image_data = array())
      {

        //$data1 = explode('/',$imge_data);
        //$data2 = in_array("konnect1", $data1);          

        $data = array(

                'ename' => $this->input->post('ename'),
                'eimg' => $this->input->post('eimg'),
                'edat_time' => $this->input->post('edat_time'),
                'evenue' => $this->input->post('evenue'),
                'sch_name' => $this->input->post('sch_name0'),
                'speaker_name' => $this->input->post('speaker_name0'),
                'sch_stime' => $this->input->post('sch_stime0'),
                'sch_etime' => $this->input->post('sch_etime0'),
                'sch_venue' => $this->input->post('sch_venue0'),
                'etype' => $this->input->post('etype'),
                'ecategory' => $this->input->post('ecategory'),             
                'agenda_desc' => $this->input->post('agenda_desc'),
                'eimg' => $image_data['full_path']
                );

        $result = $this->db->insert('event',$data);

        if($result == true)
            return true;
        else
            echo "Error in first row";
      }

      public function insert1($c)
      {
            for($i=0; $i<=$c; $i++)  
            {
                 $sql = array(
                        'sch_name' => $this->input->post('sch_name')[$i],
                        'speaker_name' => $this->input->post('speaker_name')[$i],
                        'sch_stime' => $this->input->post('sch_stime')[$i],
                        'sch_etime' => $this->input->post('sch_etime')[$i],
                        'sch_venue' => $this->input->post('sch_venue')[$i]
                    ); 

                    //$sql = "INSERT INTO event(sch_name,speaker_name,sch_stime,sch_etime,sch_venue) VALUES(($this->input->post('sch_name')[$i]),($this->input->post('speaker_name')[$i]),($this->input->post('sch_stime')[$i]),($this->input->post('sch_etime')[$i]),($this->input->post('sch_venue')[$i]))";

                    $res = $this->db->insert('event',$sql);

            } 

            if ($res == true)
                return true;
            else
                echo "Error from first row";
      }



      public function select($lastid)  
      {  
        //data is retrive from this query  
        $query = $this->db->get('event');  
        return $query;  
      }


   }

?>

for reference, attached model code also.

According to the error message, it seems PHP is unable to find the directory.

Please use PHP function is_dir() to first validate if PHP can recognize the path as a folder.

Once it returns true, you can proceed to use it.

Also in your upload path, you have started with / which would mean that your project is placed in root of OS and I don't think that location would be correct.

From the terminal cd to your project directory and run command pwd and get the current working directory and then use the proper upload path after taking into consideration the location of the project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM