使用codeigniter将数据和图像插入db

时间:2018-02-10 06:02:53

标签: php codeigniter

我现在正在创建一个简单的邮件系统。我有一个邮件撰写部分 其中我有3个文本字段和一个fileupload.I想要插入在textarea中输入的数据以及选择的图像。但是我无法插入数据。 请帮帮我

我的控制器

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

class Dashboard_controller extends CI_Controller {

    public function composepageview()
    {
        $this->load->view('mail_compose');
    }

    public function emailcompose()
    {


        $config['upload_path']          = './uploads/';
        $config['allowed_types']        = 'gif|jpg|png|pdf|doc';
        $this->load->library('upload', $config);

                if ($this->upload->do_upload())
                {
                   $data = $this->input->post();
                   $image = $this->upload->data();
                   $to= $this->input->post('to');
                   $subject= $this->input->post('subject');
                   $content= $this->input->post('content');
                   $file=base_url("uploads/".$image['raw_name'].$image['file_ext']);
                   $data['filepath']=$file;
                   $data=array(
                      "to"=>$to,
                      "subject"=>$subject,
                      "content"=>$content,
                      "filepath"=>$data['filepath']
                     );
                      $this->Login_model->compose($data);

                }
                else
                {
                     echo "fail";
                }
    }       
}
?>

我的模特

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Login_model extends CI_Model {

//Composing mail    
    public function compose($data)
    {
         $this->db->insert('compose', $data);
    }

}   

我的观点

                <div class="compose-mail">
                        <form role="form-horizontal" method="post" action="<?php echo base_url();?>index.php/Dashboard_controller/emailcompose">
                            <div class="form-group">
                                <label for="to" class="">To:</label>
                                <input type="text"  name="to" class="form-control">


                            </div>

                            <div class="form-group">
                                <label for="subject" class="">Subject:</label>
                                <input type="text" name="subject" class="form-control">
                            </div>

                            <div class="compose-editor">
                                <textarea class="wysihtml5 form-control" name="content" rows="9"></textarea>

                                <input type="file" name="userfile" class="default">

                            </div>
                            <div class="compose-btn">
                                <button class="btn btn-primary btn-sm"><i class="fa fa-check"></i> Send</button>
                                <button class="btn btn-sm"><i class="fa fa-times"></i> Discard</button>
                            </div>

                        </form>
                    </div>  

1 个答案:

答案 0 :(得分:1)

您遗失了<iframe src="https://mywebsite.com/files" ></iframe> ,但未在表单中添加enctype="multipart/form-data"

button type="submit"

希望这可以帮到你。