无法使用codeigniter将上载的图像路径存储在数据库中

时间:2017-04-12 07:32:29

标签: php codeigniter image-uploading

我在google上阅读类似的查询,在那里我读到了检查文件是否可写,然后使用chmod()函数设置权限,但我也尝试过,它没有用。我想将图像路径存储在数据库中,并将图像移动到uploads文件夹。图像的路径如下: C:/xampp/htdocs/konnect1/uploads/Hydrangeas1.jpg 在使用chmod()时,我得到警告为“消息:chmod():没有这样的文件或目录”。 请帮忙,我现在应该改变什么。

控制器页面 - > admin_c.php 发布函数,写入图像上传代码。

          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";



            }
           }
        }   


      }

模型页面 - > 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;  
      }


   }

?>

供参考,附加型号代码。

1 个答案:

答案 0 :(得分:1)

根据错误消息,似乎PHP无法找到该目录。

请使用PHP函数is_dir()首先验证PHP是否可以将路径识别为文件夹。

一旦返回true,您就可以继续使用它。

同样在您的上传路径中,您已经开始使用/这意味着您的项目位于操作系统的根目录中,我不认为该位置是正确的。

从终端cd到项目目录并运行命令pwd并获取当前工作目录,然后在考虑项目位置后使用正确的上传路径。