gpt4 book ai didi

php - Codeigniter : I am trying to submit form either if user chooses any image to upload or not choose. 这可能吗?

转载 作者:搜寻专家 更新时间:2023-10-31 21:25:16 25 4
gpt4 key购买 nike

现在,当我选择要上传的图片时,提交表单效果很好。但是即使没有选择任何图像,我也尝试提交相同的表格...

Controller

function create(){
$this->data['categories'] = $this->News_model->getNewsCategories();
$this->data['status'] = $this->status;

if($this->input->post()){

$title =$this->input->post('title');
$slug =$this->input->post('slug');
$desc =$this->input->post('desc');
$category_id =$this->input->post('category');
$upload_path = FCPATH.'assets/uploads/news';
$field_name ='image';
$temp_file_name = $this->file[$field_name]['name'];
$file_name = time().'_'.$this->randomString(10).'.'.$this->getFileExtension($temp_file_name);

if(!$this->uploadImage($upload_path,$file_name,$field_name)){
$this->session->set_flashdata('error', $this->file_error);
}
else{
$this->createThumbnail($this->file_data['full_path'], $upload_path.'/thumb/'.$file_name,75,50);
if(!$this->News_model->insertNews($title,$slug,$desc,$category_id,$file_name)){
$this->session->set_flashdata('error', 'Record couldn\'n inserted. Please try again.');
}
else{
$this->session->set_flashdata('success', 'News created.');
redirect('admin/news');
}
}


}
$this->load->view('admin/template/header');
$this->load->view('admin/news/create',$this->data);
$this->load->view('admin/template/footer');
}

如果有人想为这两种情况提交表格,请帮助我解决这个问题。谢谢……

最佳答案

如果文件名的条件不为空,试试这段代码就放一个

    function create(){
$this->data['categories'] = $this->News_model->getNewsCategories();
$this->data['status'] = $this->status;

if($this->input->post()){

$title =$this->input->post('title');
$slug =$this->input->post('slug');
$desc =$this->input->post('desc');
$category_id =$this->input->post('category');
$file_name = $_FILES['image']['name'];

if(!empty($file_name)){

$upload_path = FCPATH.'assets/uploads/news';
$field_name ='image';
$temp_file_name = $this->file[$field_name]['name'];
$file_name = time().'_'.$this->randomString(10).'.'.$this->getFileExtension($temp_file_name);

if(!$this->uploadImage($upload_path,$file_name,$field_name)){
$this->session->set_flashdata('error', $this->file_error);
}
else{
$this->createThumbnail($this->file_data['full_path'], $upload_path.'/thumb/'.$file_name,75,50);
}
}
if(!$this->News_model->insertNews($title,$slug,$desc,$category_id,$file_name)){
$this->session->set_flashdata('error', 'Record couldn\'n inserted. Please try again.');
}
else{
$this->session->set_flashdata('success', 'News created.');
redirect('admin/news');
}
}



$this->load->view('admin/template/header');
$this->load->view('admin/news/create',$this->data);
$this->load->view('admin/template/footer');
}

关于php - Codeigniter : I am trying to submit form either if user chooses any image to upload or not choose. 这可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37591695/

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