在没有活动记录codeigniter的情况下将图像上传并存储到mysql时出现错误。
显示
消息:非法字符串偏移' file_ext'
不仅是file_ext,还有file_size。这个问题只发生在我不使用下面这段代码的活动记录时:
<?php
if ($_FILES['userfile']['error'] <> 4)
{
$nmfile = $this->input->post('name');
$config['upload_path'] = './assets/images/user/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = '2048'; // 2 MB
$config['max_width'] = '2000'; //pixels
$config['max_height'] = '2000'; //pixels
$config['file_name'] = $nmfile;
$this->load->library('upload', $config);
if (!$this->upload->do_upload())
{
$this->create();
}
else
{
$userfile = $this->upload->data();
$thumbnail = $config['file_name'];
$config['image_library'] = 'gd2';
$config['source_image'] = './assets/images/user/'.$userfile['file_name'].'';
// membuat thumbnail
$config['create_thumb'] = TRUE;
// rasio resolusi
$config['maintain_ratio'] = FALSE;
// lebar
$config['width'] = 150;
// tinggi
$config['height'] = 150;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$id = $this->input->post('id');
$name = $this->input->post('name');
$username = $this->input->post('username');
$psw1 = $this->input->post('psw1');
$psw2 = $this->input->post('psw2');
$usertype = $this->input->post('usertype');
$userfile = $nmfile;
$userfile_type = $userfile['file_ext'];
$userfile_size = $userfile['file_size'];
$sql = $this->db->query("INSERT INTO user (id, name, username, psw1, psw2, usertype, userfile, userfile_type, userfile_size)
VALUES ('$id', '$name', '$username', password('$psw1'), password('$psw2'), '$usertype', '$userfile', '$userfile_type', '$userfile_size') ");
$this->session->set_flashdata('message', '<div class="alert alert-success alert">Data berhasil dibuat</div>');
redirect(site_url('auth/user'));
}
}
?>
任何帮助都会如此苛刻,谢谢
答案 0 :(得分:0)
我相信你可以用如下的可爱风格来实现你的成果:
if (!empty($_FILES)) {
$data = array(
'id' => $this->input->post('id'),
'name' => $this->input->post('name'),
'username' => $this->input->post('username'),
'psw1' => $this->input->post('psw1'),
'psw2' => $this->input->post('psw2'),
'usertype' => $this->input->post('usertype')
);
$upload = $this->imageUpload();
if (!$upload) {
$this->create();
} else {
$data['userfile'] = $upload['upload_data']['file_name'];
$data['userfile_type'] = $upload['upload_data']['file_ext'];
$data['userfile_size'] = $upload['upload_data']['file_size'];
$this->db->insert('user', $data);
$this->session->set_flashdata('message', '<div class="alert alert-success alert">Data berhasil dibuat</div>');
redirect(site_url('auth/user'));
}
}
使用这些功能上传图片并调整图片大小
function imageUpload() {
$config = array(
'upload_path' => "uploads",
'allowed_types' => "*",
'overwrite' => true,
'max_size' => "5048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "3000",
'max_width' => "3000"
);
$this->upload->initialize($config);
$this->load->library('upload', $config);
if ($this->upload->do_upload('userfile')) {
$response = array('upload_data' => $this->upload->data());
$this->doResize($response['upload_data']['file_name']);
return $response;
} else {
$error = array('error' => $this->upload->display_errors());
return false;
// var_dump($error); die(); // check errors
}
}
function doResize($filename) {
$sourcePath = './assets/images/user/' . $filename;
$targetPath = './assets/images/user/thumb_' . $filename;
$config_manip = array(
'image_library' => 'gd2',
'source_image' => $sourcePath,
'new_image' => $targetPath,
'maintain_ratio' => true,
'width' => 100,
'height' => 100
);
$this->image_lib->initialize($config_manip);
$this->load->library('image_lib', $config_manip);
if (!$this->image_lib->resize()) {
// Check errors
echo $this->image_lib->display_errors();
die();
}
}