[英]Codeigniter image upload and path to database
我在尝试在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->upload->do_upload()
那是负责执行上载的功能,您的代码中没有该功能,您可能需要阅读以下内容:文件上载类
UPDATE
您的代码应如下所示:
控制器:
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);
}
问题出在您使用上传库的代码中。 您使用了错误的功能来上传文件。 下面是正确的代码:
$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
}
此代码应该起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.