doter1995 2017-02-28 14:56
浏览 15

Codeigniter上传图片选择

I have a form including different inputs and file upload input. I want the users if they want to upload images then upload, but if they don't want to upload. Don't give an error.

if($_POST){         
    $config['upload_path']          = 'images/tmp';
    $config['allowed_types']        = 'gif|jpg|png';
    $config['max_size']             = 2048;
    $config['min_width']            = 480;
    $config['min_height']           = 360;

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

    if (!$this->upload->do_upload('userfile')) {
        $error = array('error' => $this->upload->display_errors());
        //Sending this $error to view and if a user didnt upload image and just filled
        //the other inputs it shows error. but i dont want that.
    } else { }
} else {
    redirect(site_url());
}
  • 写回答

3条回答 默认 最新

  • dongmu1996 2017-02-28 15:24
    关注

    You are on right track. Just delete or comment this line

    $error = array('error' => $this->upload->display_errors());

    You might be send your $error to view file like below:

    $this->load->view('upload_form', $error);

    So Don't send your value to view file. Just call your view when image successfully uploaded below:

    
                if ( ! $this->upload->do_upload('userfile') )
                {
    
                    // $error = array('error' => $this->upload->display_errors());
                    /*Delete or comment this line. Do your other stuff here if image not uploaded.*/
    
                }else{
                       $data = array('upload_data' => $this->upload->data());
                       $this->load->view('upload_success', $data);
                       /* This is example. You can do anything on successfully image upload.*/
                }
    

    You can refer codeigniter manual https://www.codeigniter.com/userguide3/libraries/file_uploading.html

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行