gpt4 book ai didi

php - Codeigniter 上传文件不起作用

转载 作者:行者123 更新时间:2023-12-04 00:39:46 24 4
gpt4 key购买 nike

function submit_article() {
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<p style="color:red">', '<br/></p>');

$my_rules = array(
array(
'field' => 'title',
'label' => 'Title',
'rules' => 'required|min_length[5]|max_length[20]|xss_clean'
),
array(
'field' => 'additionalUpload',
'label' => 'Additional Upload',
'rules' => 'callback_is_image'
)
);

$this->form_validation->set_rules($my_rules);

if ($this->form_validation->run() == FALSE) {
//ERROR
$data['title'] = ucfirst('submit Article');
$this->load->view('templates/header', $data);
$this->load->view('submit_article', $data);
$this->load->view('templates/footer', $data);
} else {
//SUCCESS
$data['title'] = ucfirst('article Submitted');
$this->load->view('templates/header', $data);
$this->load->view('forms_view/submit_article_success', $data);
$this->load->view('templates/footer', $data);
}
}

function is_image($value) {

$config['upload_path'] = './public/uploads/';
$config['allowed_types'] = 'gif|jpg|png|pdf|tiff';
$config['max_size'] = '2048';
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['remove_spaces'] = true;

$this->load->library('upload', $config);

if (!$this->upload->do_upload()) {
$this->form_validation->set_message('is_image', $this->upload->display_errors('<p style="color:red">', '<br/></p>'));
return FALSE;
} else {
$this->upload->data();
return TRUE;
}
}

大家好,这是我在 codeigniter 中处理多部分表单数据的 Controller 功能代码,实际上字段 additionalUpload 不是必填字段,但我希望如果用户在文件类型的 additionalUpload 字段中上传文件,则对其进行验证,当我运行上面的代码并单击提交按钮而不选择任何文件时,它会显示错误“您没有选择要上传的文件。”我不想要,因为这不是必填字段,这是我的第一个问题..

第二个是,当我选择一个文件并单击提交按钮时,它再次显示“您没有选择要上传的文件。”。

注意:我刚刚在这里显示了我的表单的两个字段,即 title 和 additionalUpload,但我总共有 9 个字段。

提前致谢,请帮助任何人。

最佳答案

首先你必须输入文件字段的名称。

<input type="file" name="image"/>
$this->upload->do_upload('image');

其次,你不能让 max_width 和 max_height 为 0

$config['max_width']    = '2048';
$config['max_height'] = '2048';

先试一下再看

对于验证字段文件:

if($_FILES['you_field_name']['tmp_name']){

//your code
}

一句问候

关于php - Codeigniter 上传文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20179329/

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