控制器
public function addemployee()
{
$config['upload_path'] = './images/employee/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('include/headerforms');
$this->load->view('include/aside');
$this->load->view('template/forms/employeeforms',$error);
$this->load->view('include/footerforms');
}
else
{
$data = array('upload_data' => $this->upload->data('userfile'));
$data = [
'userfile' => './images/employee/' . $file['file_name'],
'userfile' => $this->input->post('userfile'),
'firstname' => $this->input->post('firstname'),
'firstname' => $this->input->post('firstname'),
'middlename'=> $this->input->post('middlename'),
'lastname'=> $this->input->post('lastname'),
'company'=> $this->input->post('company'),
'department'=> $this->input->post('department'),
'position'=> $this->input->post('position'),
'address'=> $this->input->post('address'))
];
$this->ci_db_model->addemployee($data);
此部分连接到我的数据库
$this->session->set_flashdata('message','New image has been added..');
$this->load->view('include/header2');
$this->load->view('include/aside');
$this->load->view('template/employee');
$this->load->view('include/footer2');
}
}
查看
<!-- Name -->
<?php echo form_open_multipart('home/addemployee'); ?>
<div class="box-body">
<div class="form-group">
<label for="exampleInputFile">Photo</label>
<input type="file" name"userfile" id="exampleInputFile" required>
</div>
<div class="row">
<div class="col-xs-4">
<input name="firstname" type="text" class="form-control" placeholder="First Name" required>
</div>
<div class="col-xs-4">
<input name="middlename" type="text" class="form-control" placeholder="Middle Name" required>
</div>
<div class="col-xs-4">
<input name="lastname" type="text" class="form-control" placeholder="Last Name" required>
</div>
</div>
<input type="submit" name="submit" class="btn btn-primary pull-right" value="Submit">
<?php echo form_close(); ?>
编程语言:PHP Codeigniter 服务器:XAMPP
我和BEGINNER !!
问题:如何将图像上传到数据库?
当我点击提交按钮时出现错误&#34;您没有选择要上传的文件。&#34;。
我的代码出了什么问题?
感谢先生,请帮助任何人。
答案 0 :(得分:1)
控制器
if ((isset($_FILES['userfile']['name'])) && ($_FILES['userfile']['name'] != "")) {
$file = "userfile";
$path = './uploads/file/' ;
$uploadedimages = $this->upload_image($file, $path, 1);
$data['image'] = $uploadedimages['file_name'];
}
/ *上传文件功能* /
public function upload_image($file, $path, $thumb) {
/* Upload file in posted */
$this->load->library('image_lib');
$this->load->library('upload');
$image_upload_folder = $path;
if (!file_exists($image_upload_folder)) {
mkdir($image_upload_folder, DIR_WRITE_MODE, true);
}
@chmod($image_upload_folder, 0777);
$this->upload_config = array(
'upload_path' => $image_upload_folder,
'allowed_types' => '*',
'remove_space' => TRUE,
'encrypt_name' => TRUE,
);
$this->upload->initialize($this->upload_config);
if (!$this->upload->do_upload($file)) {
$upload_error = $this->upload->display_errors();
echo ($upload_error);
} else {
$file_info = $this->upload->data();
if ($thumb == 1) {
$this->image_lib->clear();
$config['image_library'] = 'GD2';
$config['source_image'] = $image_upload_folder . '/' . $file_info['file_name'];
$config['create_thumb'] = TRUE;
$config['thumb_marker'] = '_thumb';
$config['master_dim'] = 'width';
$config['quality'] = 100;
$config['maintain_ratio'] = TRUE;
$config['width'] = 150;
$config['height'] = 150;
$config['new_image'] = $file_info['file_name'];
//$this->image_lib->clear();
//move_uploaded_file($value['full_path'], '/uploads/' . $newimagename);
$this->image_lib->initialize($config);
//$this->load->library('image_lib', $config);
if (!$this->image_lib->resize()) {
$this->image_lib->clear();
$data['file_name'] = $file_info['file_name'];
$data['file_thumb'] = $file_info['file_name'];
} else {
$upload_error = $this->image_lib->display_errors();
$data['file_name'] = $file_info['file_name'];
$thmb = explode(".", $data['file_name']);
$data['file_thumb'] = $thmb[0] . "_thumb." . $thmb[1];
}
return $data;
} else {
return $file_info['file_name'];
}
}
}