我需要帮助将图片上传到文件夹和数据库。似乎我只能将其发送到数据库,而不能将其上传到文件夹。代码有什么问题吗?
管理员.php(控制器(
public function input_siswa(){
$config['upload_path'] = './gambarfolder/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload');
$this->upload->initialize($config);
$location=base_url().'/gambarfolder/';
$pict=$location.$data_imge;
$nama_siswa = $this->input->post('nama_siswa');
$nis = $this->input->post('nis');
$id_jurusan = $this->input->post('id_jurusan');
$data = array(
'nama_siswa'=>$nama_siswa,
'nis'=>$nis,
'id_jurusan'=>$id_jurusan,
'gambar'=>$pict
);
$this->m_model->create('siswa',$data);
redirect(base_url('index.php/admin/tampil_siswa'));
}
使用上传库在代码点火器中上传图像。
public function input_siswa(){
$config['upload_path'] = './gambarfolder/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$location=base_url().'/gambarfolder/';
$pict=$location.$data_imge;
$this->load->library('upload', $config);
if ($this->upload->do_upload('userfile'))
{
$nama_siswa = $this->input->post('nama_siswa');
$nis = $this->input->post('nis');
$id_jurusan = $this->input->post('id_jurusan');
$data = array(
'nama_siswa'=>$nama_siswa,
'nis'=>$nis,
'id_jurusan'=>$id_jurusan,
'gambar'=>$pict
);
$this->m_model->create('siswa',$data);
redirect(base_url('index.php/admin/tampil_siswa'));
// redirect succsess page when image upload succsessfully.
}else{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload', $error);
// return image upload error.
}
}
首先检查您的文件夹 gambarfolder 是否位于 Codigniter 应用程序的根目录中
第二次签入表单标签,添加 enctype="多部分/表单数据">
并添加该行以将图像上传到 gambarfolder 文件夹
$this->load->library('upload', $config);
if($this->upload->do_upload('userfile'))
{
}
其中用户文件是文件标签的名称,例如
<input type="file" name="userfile" size="20" />
$config是您的配置数组
并检查 https://www.codeigniter.com/userguide3/libraries/file_uploading.html
初始化配置后需要添加以下行
$this->upload->do_upload('userfile')