如何使用mysql数据库在codeigniter中上传图像

时间:2017-11-15 07:28:58

标签: php jquery twitter-bootstrap-3 codeigniter-3

请注意:所有数据都已正确插入。                但是,如果我借助于上传图像                $ this-> upload-> do_upload();并验证id,
               这不正常。你可以建议..

//Controller

public function saveEmployeea()
    {

      $config = [ 'upload_path'=>'./uploads',
          'allowed_types'=>'gif|jpg|png|jpeg'];

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

      $this->form_validation->set_rules('dept_name', 'Department', 'required');

      $this->form_validation->set_rules('firstname', 'First Name','required');

      $this->form_validation->set_rules('middlename', 'Middle Name','required');

      $this->form_validation->set_rules('lastname', 'Last Name','required');

      $this->form_validation->set_rules('gendar', 'Gendar','required');

      $this->form_validation->set_rules('address1', 'Local Address','required');

      $this->form_validation->set_rules('address2', 'Permant Address','required');

      $this->form_validation->set_rules('nationality', 'Nationality','required');


      $this->form_validation->set_rules('date_of_joining', 'Joinig Date','required');

      $this->form_validation->set_rules('current_designation', 'Designation_Curnt','required');

      // $this->form_validation->set_rules('profile_image', 'Please Upload image','required');

      $this->form_validation->set_rules('prof_email_id', 'Professional Email Id','required|valid_email');

      $this->form_validation->set_rules('personal_email', 'Personal Email Id','required|valid_email');
      $this->form_validation->set_rules('personal_no', 'Personal Contact No','required');

      $this->form_validation->set_rules('emergency_no', 'Emergency No','required');

      $this->form_validation->set_rules('highest_qualification', 'Highest Qualificatio','required');

      $this->form_validation->set_rules('year_of_passing', 'Year Of Passing','required');

      $this->form_validation->set_rules('university', 'University','required');

      $this->form_validation->set_rules('no_experience', 'No Experience','required');

      $this->form_validation->set_rules('pre_compony', 'Previus Compony','required');

      $this->form_validation->set_rules('pre_designation', 'Previus Designation','required');
      $this->form_validation->set_rules('pre_organisation_location', 'Previus organisation_location','required');




      if ( $this->form_validation->run() && $this->upload->do_upload()) 
      {

        $data = $this->input->post();


        // insert images in data variables by using upload object oprater.
        $upload_info = $this->upload->data();



        //set path for images store 
        $path = base_url("uploads/".$upload_info['raw_name'].$upload_info['file_ext']);

        $data['profile_image'] = $path;

        // echo '<pre>';
        // print_r($data);
        // echo '</pre>';
        // var_dump($data);
        // exit();  

        // Load Model to Call Model class
        $this->load->model('Loginm');


        if($this->Loginm->insertEmployee($data))
        {
          $this->session->set_flashdata('employee_details','Employee Details Added Successfully');
        }   
        else
        {
          $this->session->set_flashdata('employee_details','Failed To Added Employee Details.');
        }
          return redirect('Employee/EmployeeList'); 

      }
      else
      {
        $upload_error = $this->upload->display_errors();
        $this->createEmployee();
      }
    }


// Model

public function insertEmployee($data)
{
    return $this->db->insert('employee',$data);
}`enter code here`

2 个答案:

答案 0 :(得分:0)

首先,

     $this->upload->do_upload('userfile')

这里&#39; userfile&#39;在input元素的name属性中。

     <form action="url" enctype="multipart/form-data" method="post">
     <input type="file" name="userfile" accept="image/*">

确保使用: ENCTYPE =&#34;多部分/格式数据&#34; 要么 CI 3对应: echo form_open_multipart();

第二

上传目录

您需要为上传的图片提供目标目录。在CodeIgniter安装的根目录下创建一个名为uploads的目录,并将其文件权限设置为777。

您可能在您设置的上传目录中遇到文件权限问题。您应该为上传目录设置适当的权限。

参考文献:

https://www.codeigniter.com/userguide3/libraries/file_uploading.html

答案 1 :(得分:0)

试试此代码

$config['upload_path']    = 'd:/www/project_name/uploads/'; //document root path
$config['allowed_types']  = 'jpg|gif|png';
$config['file_name']      = $_FILES['profile_image']['name'];

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

if (!$this->upload->do_upload('profile_image'))
{
        // case - failure
        echo $this->upload->display_errors();
}
else
{
    // case - SUCCESS
}