如何在codeigniter上传图像

时间:2015-10-14 07:20:38

标签: php codeigniter

嘿我正在尝试将图片上传到我的资源文件夹中。但我无法做到这一点。实际上我想保存图像的名称为"最后插入的ID" 。 我不知道错误是什么。请帮帮我

   $.ajax({
                url: base_url+"register/reg_submit/",
                data: $('#career_submit_form').serialize(),//returns all cells' data
                dataType: 'json',
                type: 'POST',
                success: function (res) {
                    alert(res.result)
   },

在我的控制器中 reg_submit

      $application_insert = $this->career->insert_student_application($personal_details,$parent_details,$other_details);

    $config['upload_path'] = './application/assets/images/user_image/';
    $config['allowed_types'] = 'jpg'; 
    $config['file_name'] = $application_insert.'.jpg'; 
    $this->load->library('upload', $config); 
    if (!$this->upload->do_upload('userfile'))
    {     
        echo $this->upload->display_errors();  

    } 

2 个答案:

答案 0 :(得分:0)

试试这个:

public function file_upload(){
              $files = $_FILES;
                $cpt = count($_FILES['userfile']['name']);
                 for($i=0; $i<$cpt; $i++)
                {
                $_FILES['userfile']['name']= $files['userfile']['name'][$i];
                $_FILES['userfile']['type']= $files['userfile']['type'][$i];
                $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
                 $_FILES['userfile']['error']= $files['userfile']['error'][$i];
                 $_FILES['userfile']['size']= $files['userfile']['size'][$i];
                $this->upload->initialize($this->set_upload_options());
                $this->upload->do_upload();
                $fileName = $_FILES['userfile']['name'];
                 $images[] = $fileName;
}
  $fileName = implode(',',$images);
  $this->my_model->upload_image($fileName);
}
private function set_upload_options()
  { 
  // upload an image options
         $config = array();
         $config['upload_path'] = './upload/'; //give the path to upload the image in folder
         $config['allowed_types'] = 'gif|jpg|png';
          $config['max_size'] = '0';
         $config['overwrite'] = FALSE;
  return $config;
  }

有关如何在codeigniter中上传图片的更多信息,请尝试:http://w3code.in/2015/09/upload-file-using-codeigniter/

答案 1 :(得分:0)

请按照以下步骤

1. check $application_insert variable return anything?

2. change './application/assets/images/user_image/' to "application/assets/images/user_image/"

3. make writable all the folders assests, images and user_image

4. check spelling of the folders are same what you given in the upload path