gpt4 book ai didi

php - 非法字符串偏移上传并将图像存储到mysql而没有事件记录codeigniter

转载 作者:行者123 更新时间:2023-11-29 18:38:15 25 4
gpt4 key购买 nike

在没有事件记录 Codeigniter 的情况下将图像上传并存储到 mysql 时出现错误。

显示

Message: Illegal string offset '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'));
}
}
?>

如有任何帮助,我们将非常感激,谢谢

最佳答案

我相信您可以通过如下可爱的风格实现您的结果:

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

关于php - 非法字符串偏移上传并将图像存储到mysql而没有事件记录codeigniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45114523/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com