Image: Submit -6ren">
gpt4 book ai didi

php - Codeigniter 文件上传错误

转载 作者:行者123 更新时间:2023-12-05 08:43:00 25 4
gpt4 key购买 nike

我正在尝试在 codeigniter 中上传图片。

这是我的查看文件代码。

<form action="<?php echo site_url('pages/data_submitted') ?>" method="get" enctype="multipart/form-data">
Image: <input type="file" name="image"/>
<button>Submit</button>
</form>

这是我的 Controller 代码。

class Pages extends CI_Controller 
{
public function data_submitted(){
$config['upload_path'] = "img/";
$this->load->library('upload',$config);
$this->upload->do_upload();
$finfo=$this->upload->data();
$data = $this->upload->display_errors();

$this->load->model('user_model');
$this->user_model->insert_item($data);
}
}

这是我的模型代码

<?php
class User_model extends CI_Model {
function __construct(){
/* Call the Model constructor */
parent::__construct();
}


public function insert_item($item){

print_r($item);
}
}
?>

这段代码有什么问题...这里我传递 $data 只是为了检查是否发生任何错误。它显示“您没有选择要上传的文件。”即使我选择一个文件。请帮助我。

最佳答案

引用这段代码。这肯定对你有用

public function uploadImage() {
$this->load->helper(array('form', 'url'));
$config['upload_path'] = 'assets/images/b2bcategory';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '2024';
$config['max_height'] = '1768';
$config['width'] = 75;
$config['height'] = 50;
if (isset($_FILES['catimage']['name'])) {
$filename = "-" . $_FILES['catimage']['name'];
$config['file_name'] = substr(md5(time()), 0, 28) . $filename;
}
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$field_name = "catimage";
$this->load->library('upload', $config);
if ($this->input->post('selsub')) {
if (!$this->upload->do_upload('catimage')) {
//no file uploaded or failed upload
$error = array('error' => $this->upload->display_errors());
} else {
$dat = array('upload_data' => $this->upload->data());
$this->resize($dat['upload_data']['full_path'], $dat['upload_data']['file_name']);
}
$ip = $_SERVER['REMOTE_ADDR'];
if (empty($dat['upload_data']['file_name'])) {
$catimage = '';
} else {
$catimage = $dat['upload_data']['file_name'];
}
$data = array(
'ctg_image' => $catimage,
'ctg_dated' => time()
);
$this->b2bcategory_model->form_insert($data);

}
}

关于php - Codeigniter 文件上传错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35285028/

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