douzhan1031 2017-04-12 07:32
浏览 50

无法使用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.

  • 写回答

1条回答 默认 最新

  • doushishi6513 2017-04-12 07:40
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件