codeigniter,表单图片上传

codeigniter, form image upload

我有一个简单的表格,我添加了图片上传功能。

但是我在写入数据库时​​遇到问题。

图片已上传至文件夹,除图片外的所有操作均已写入数据库,但由于未将图片写入数据库,因此表单未成功。

这是我的控制器:

public function send_Basvur_message(){

    $config['upload_path']          = './panel/uploads/basvur_v/';
    $config['allowed_types']        = 'jpg|png';
    $config['max_size']             = 4096;
    $config['overwrite']            = false;
    $config['encrypt_name']         = false;
    $config['remove_space']         = true;

    $this->upload->initialize($config);

    $upload = $this->upload->do_upload('avatara');

    $this->load->library("form_validation");
    $this->load->model("Basvur_model");

    $this->form_validation->set_rules("name", "Ad", "trim|required");
    $this->form_validation->set_rules("email", "E-posta", "trim|required|valid_email");
    $this->form_validation->set_rules("subject", "Konu", "trim|required");
    $this->form_validation->set_rules("message", "Mesaj", "trim|required");
    $this->form_validation->set_rules("avatar", "avatar", "trim|required");
    $this->form_validation->set_rules("captcha", "Doğrulama Kodu", "trim|required");

    if($this->form_validation->run() === FALSE) {
        // TODO Alert...
        $this->session->set_flashdata('info','Action Completed');
        redirect(base_url("basvuru"));
    } else {

        $insert = $this->Basvur_model->add(
            array(
                "name"          => $this->input->post("name"),
                "email"         => $this->input->post("email"),
                "message"       => $this->input->post("message"),
                "subject"       => $this->input->post("subject"),
                "avatar"        => $this->input->post("avatar"),
                "created_at"    => date("Y-m-d H:i:s")
            )
        );

        if($this->session->userdata("captcha") == $this->input->post("captcha")){

            $name = $this->input->post("name");
            $email = $this->input->post("email");
            $subject = $this->input->post("subject");
            $message = $this->input->post("message");
            $avatar = $this->input->post("avatar");

            $email_message = "{$name} isimli ziyaretçi. Başvuru Yaptı <br><b>Mesaj : </b> {$message} <br> <b>E-posta : </b> {$email}";

            if(send_email("", "Yeni Aday başvurusu | $subject", $email_message)){
                $this->session->set_flashdata('success','Action Completed');
                redirect(base_url("basvuru"));
                // TODO Alert..
            } else {
                $this->session->set_flashdata('success','Action Completed');
                redirect(base_url("basvuru"));
                // TODO Alert..
            }

        } else {
            //başarısıs
        $this->session->set_flashdata('error','Action Not Completed');
        redirect(base_url("basvuru"));

        }}}

您不能使用 $this->input->post("avatar") 作为文件名。使用 $this->upload->do_upload('avatara') 上传文件后,您必须使用 $this->upload->data() 获取文件名。获取变量中 $this->upload->data() 的值并发送到模型。

you can get image name using 

$config['file_name'] =$_FILES['avatar']['name'];

$config['upload_path'] = './images/contents/'; /* NB! create this dir! */
$config['allowed_types'] = 'jpg|png|jpeg';
$config['max_size']  = '0';
$config['max_width']  = '0';
$config['max_height']  = '0';

$this->load->library('upload', $config);
if(!$this->upload->do_upload('avatar'))
{
 $data['msg'] = $this->upload->display_errors();
 $this->session->set_flashdata('msg_succ',$data['msg']);


}
else
{
  $data = $this->upload->data();
  $uploadedImages['image'] = $data['file_name'];
  $image = $uploadedImages['avatar'];

    here you can update image name in database`enter code here`             

}