简体   繁体   English

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

[英]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. 我在google上读过类似的查询,在那儿我读到有关检查文件是否可写,然后使用chmod()函数设置权限的信息,但是我也尝试过,但没有成功。 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". 图像的路径将为:C:/xampp/htdocs/konnect1/uploads/Hydrangeas1.jpg使用chmod()时,出现警告消息为“消息:chmod():无此类文件或目录”。 please help as what should I change now. 请帮忙,因为我现在应该更改什么。

Controller page->admin_c.php Posting the function, where image upload code is written. 控制器页面-> 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";



            }
           }
        }   


      }

Model Page->admin_m.php 模型页-> 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. 根据错误消息,似乎PHP无法找到目录。

Please use PHP function is_dir() to first validate if PHP can recognize the path as a folder. 请首先使用PHP函数is_dir()首先验证PHP是否可以将该路径识别为文件夹。

Once it returns true, you can proceed to use it. 一旦返回true,就可以继续使用它。

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. 同样在您的上传路径中,您以/开头,这意味着您的项目位于OS的根目录中,我认为该位置不正确。

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. 从终端cd到项目目录,然后运行命令pwd并获取当前的工作目录,然后在考虑项目位置后使用正确的上载路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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