Codeigniter enctype=“multipart/form-data” 未正确保存照片

Codeigniter当我在注册表单中使用enctype=“multipart/form-data”时,照片不会保存在数据库中,而是照片会上传到上传文件夹。然后当我删除enctype=“多部分/表单数据”照片保存在数据库中,但照片不来上传文件夹。如果这是代码的问题,请执行完整代码。因为我不是一个好的开发人员。


视图


<form method="post" action="family_join" enctype="multipart/form-data" id="wizard">


    <h4 class="text-center mb-4 mt-4">Upload Photo</h4>

    <div class="form-group mt-3 mb-4">

        <div class="dropzone-wrapper">

            <div class="dropzone-desc">

                <i class="glyphicon glyphicon-download-alt"></i>

                <p>Choose an image file or drag it here.</p>

            </div>

            <input type="file" name="photo" class="dropzone">

        </div>

    </div>


    <h4 class="text-center mb-4">Please enter the zipcode</h4>

    <div class="default-form contact-form">

        <div class="form-group">

            <input type="text" name="zipcode" placeholder="Enter Zip Code" required>

        </div>

    </div>


</form>

控制器


< ? php

defined('BASEPATH') OR exit('No direct script access allowed');

Class Family_Join extends CI_Controller {

    public

    function __construct() {

        parent::__construct();

        $this->load-> helper(array('form', 'url'));

    }

    public

    function index() {

        $this->form_validation-> set_rules('zipcode', 'Zipcode', 'required|min_length[4]');

        if ($this->form_validation->run()) {

            $zipcode=$this->input->post('zipcode');

            $photo=$this->input->post('photo');

            $status = 1;

            $this->load->model('Family_Join_Model');


            $config['upload_path'] = './uploads/';

            $config['allowed_types'] = 'gif|jpg|png';

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

            $this->upload->do_upload('photo');


            $this-> Family_Join_Model->insert($zipcode, $photo, $status);

        } else {

            $this->load->view('user/family_join', array('error' => ' '));

        }

    }

}



神不在的星期二
浏览 121回答 3
3回答

守候你守候我

首先上传文件,然后从上传的文件数据中获取文件名。&nbsp; &nbsp; &nbsp; &nbsp; $config['upload_path'] = './uploads/';&nbsp; &nbsp; &nbsp; &nbsp; $config['allowed_types'] = 'gif|jpg|png';&nbsp; &nbsp; &nbsp; &nbsp; $this->load->library('upload', $config);&nbsp; &nbsp; &nbsp; &nbsp; if ($this->upload->do_upload('photo')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $file_details = $this->upload->data();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $photo = $file_details['file_name'];&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $file_upload_msg = $this->upload->display_errors('<p>', '</p>');&nbsp; &nbsp; &nbsp; &nbsp; }

莫回无

发生这种情况是因为当您将 enctype 设置为多部分/表单数据时,您的输入将作为二进制文件发送到服务器,并且二进制文件无法插入到数据库中。要将文件名插入到数据库中,请执行以下步骤。保持 enctype=multipart/form-data(这样你的文件就会上传到服务器)替换此行 :$this->Family_Join_Model->插入($zipcode, $photo, $status);与这些:$upload_data&nbsp;=&nbsp;$this->upload->data();&nbsp; $this->Family_Join_Model->insert($zipcode,&nbsp;$upload_data['file_name'],&nbsp;$status);

慕尼黑5688855

$this->upload->do_upload('photo');$upload_data = $this->upload->data();$this->Family_Join_Model->insert($zipcode, $upload_data, $status);**Model**publicfunction insert($zipcode, $upload_data, $status) {&nbsp; &nbsp; $data = array(&nbsp; &nbsp; &nbsp; &nbsp; 'zipcode' => $zipcode,&nbsp; &nbsp; &nbsp; &nbsp; 'photo' => $upload_data['file_name'],&nbsp; &nbsp; &nbsp; &nbsp; 'isActive' => $status&nbsp; &nbsp; );&nbsp; &nbsp; $sql_query=$this->db->insert('tblfm', $data);&nbsp; &nbsp; if ($sql_query) {&nbsp; &nbsp; &nbsp; &nbsp; $this->session->set_flashdata('success', 'Registration successfull');&nbsp; &nbsp; &nbsp; &nbsp; redirect('user/family_join');&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; $this->session->set_flashdata('error', 'Somthing went worng. Error!!');&nbsp; &nbsp; &nbsp; &nbsp; redirect('user/family_join');&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP