douli2063 2017-05-07 18:20
浏览 41
已采纳

Codeigniter图像上传和数据库路径

I'm having a bit of a problem trying to make an upload form in codeigniter. Not sure what I am doing wrong, everything else is getting correctly in the database. I looked in the documentation and tried several things but I'm not getting any further.. Any feedback is very much appreciated!
Thanks in advance.

model:

public function set_newstudent()
    {
        $this->load->helper('url');

        $slug = url_title($this->input->post('naam'), 'dash', TRUE);

                 $config['upload_path'] = '/file_path/';
                 $config['allowed_types'] = 'gif|jpg|png|jpeg';
                 $this->load->library('upload', $config);
                 $this->upload->data('full_path'););
                 $data_upload_files = $this->upload->data();

                 $image = $data_upload_files[full_path];

                 $data = array(

            'naam' => $this->input->post('naam'),
            'voornaam' => $this->input->post('voornaam'),
            'id' => $slug,
            'text' => $this->input->post('text'),
            'picture'=>$this->input->post('picture')
    );

        return $this->db->insert('student', $data);
    }

controller:

public function create()
            {
                $this->load->helper('form');
                $this->load->library('form_validation');

                $data['title'] = 'Create a new Student';

                $this->form_validation->set_rules('naam', 'Naam', 'required');
                $this->form_validation->set_rules('voornaam', 'Voornaam', 'required');
                $this->form_validation->set_rules('text', 'Text', 'required');

                if ($this->form_validation->run() === FALSE)
                    {
                    $this->load->view('templates/header', $data);
                    $this->load->view('students/create');
                    $this->load->view('templates/footer');

                    }
                else
                    {
                    $this->student_model->set_newstudent();
                    $this->load->view('students/success');
                    }
            }

view:

<?php echo validation_errors(); ?>


<?php echo form_open_multipart('student/create');?>

<div class="form-group">
<label for="naam">Naam</label><br>
<input type="input" name="naam" class="form-control" /><br />
</div>
<div class="form-group">
<label for="voornaam">Voornaam</label><br>
<input type="input" name="voornaam" class="form-control"/><br />
</div>
<div class="form-group">
<label for="text">Vertel iets over jezelf:</label><br>
<textarea name="text" class="form-control" rows="5"></textarea><br />
</div>

<div class="form-group">
<label for="text">Kies een profiel foto:</label><br>
<input type="file" name="userfile"  class="btn btn-default btn-file" />

</div>
<input type="submit" class="btn btn-success" name="submit" 
value="Create student" style="width:100%;margin-bottom:1%" />

</form>
  • 写回答

2条回答 默认 最新

  • dongzan1970 2017-05-07 18:46
    关注

    As i see, there is no

    $this->upload->do_upload()
    

    That's the function responsible for performing the upload and it's not there in your code,you may want to read this: File uploading Class

    UPDATE

    Your code should look something like this,

    Controller:

    public function create(){
        $this->load->helper('form');
        $this->load->library('form_validation');
        $data['title'] = 'Create a new Student';
        $this->form_validation->set_rules('naam', 'Naam', 'required');
        $this->form_validation->set_rules('voornaam', 'Voornaam', 'required');
        $this->form_validation->set_rules('text', 'Text', 'required');
        if ($this->form_validation->run() === FALSE){
            $this->load->view('templates/header', $data);
            $this->load->view('students/create');
            $this->load->view('templates/footer');
        }else{
            // Upload the files then pass data to your model
            $config['upload_path'] = '/file_path/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $this->load->library('upload', $config);
    
            if (!$this->upload->do_upload('userfile')){
                // If the upload fails
                echo $this->upload->display_errors('<p>', '</p>');
            }else{
                // Pass the full path and post data to the set_newstudent model
                $this->student_model->set_newstudent($this->upload->data('full_path'),$this->input->post());
                $this->load->view('students/success');
            }
        }
    }
    

    Model:

    public function set_newstudent($path,$post){ 
        $data = array( 
            'naam' => $post['naam'], 
            'voornaam' => $post['voornaam'], 
            'text' => $post['text'], 
            'picture'=>$path 
        ); 
    
        return $this->db->insert('student', $data); 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB代码补全插值
  • ¥15 Typegoose 中如何使用 arrayFilters 筛选并更新深度嵌套的子文档数组信息
  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的