【问题标题】:Codeigniter image upload and path to databaseCodeigniter 图像上传和数据库路径
【发布时间】:2017-05-07 18:20:14
【问题描述】:

我在尝试在 codeigniter 中制作上传表单时遇到了一点问题。 不知道我做错了什么,其他一切都在数据库中正确。我查看了文档并尝试了几件事,但我没有进一步了解。非常感谢任何反馈!
提前致谢。

型号:

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);
    }

控制器:

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');
                    }
            }

查看:

<?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>

【问题讨论】:

  • 这是什么? $this-&gt;upload-&gt;create
  • 对不起先生,我的错误,应该是 $this->upload->data('full_path');
  • 请问您使用的是什么版本的codeigniter?
  • 代码点火器版本为 3.1.4
  • 您要在 id 列中插入什么?

标签: php forms codeigniter upload


【解决方案1】:

如我所见,没有

$this->upload->do_upload()

这是负责执行上传的函数,它不在您的代码中,您可能需要阅读以下内容: File uploading Class

更新

您的代码应如下所示,

控制器:

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');
        }
    }
}

型号:

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); 
}

【讨论】:

  • 谢谢您的回答先生。我可以将它添加到函数 create() 吗?还是必须是一个单独的功能?
  • 没有create这个函数,你能告诉我你在哪里找到这个函数吗?
  • 在控制器中
  • 不,你不能那样做,你可能想用我在文档中给你的例子重写代码
  • 添加我添加的代码echo $this-&gt;upload-&gt;display_errors('&lt;p&gt;', '&lt;/p&gt;');
【解决方案2】:

问题出在您使用上传库的代码中。您正在使用错误的功能来上传文件。下面是正确的代码:

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

        // Check file uploaded or not
        if ($this->upload->do_upload('userfile')) {
            $data_upload_files = $this->upload->data();
            $image = $data_upload_files[full_path];
        } else {
            // possibly do some clean up ... then throw an error

        }

这段代码应该可以工作。

【讨论】:

  • 对不起,它不起作用,我将控制器中的代码更改为上述但我仍然没有得到结果
  • 我可以知道您在屏幕上遇到的确切错误吗?
  • 问题是,我没有收到错误消息。用户被创建,名字和姓氏字段被写入数据库,但即使附加了图片,我也看不到图片
猜你喜欢
  • 2018-05-09
  • 2013-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-03
  • 2013-07-18
  • 1970-01-01
相关资源
最近更新 更多