【问题标题】:Codeigniter Upload Image ChoiceCodeigniter 上传图片选择
【发布时间】:2017-07-19 14:48:50
【问题描述】:

我有一个包含不同输入和文件上传输入的表单。我想要用户如果他们想上传图片然后上传,但如果他们不想上传。不要报错。

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

【问题讨论】:

    标签: php codeigniter file-upload upload image-uploading


    【解决方案1】:

    你在正确的轨道上。只需删除或评论此行

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

    您可能会发送 $error 以查看如下文件:

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

    所以不要将您的值发送到查看文件。只需在以下图片成功上传后调用您的视图:

    if ( !$this->upload->do_upload('userfile') ) { // $error = array('error' => $this->upload->display_errors()); /*删除或注释这一行。如果图片未上传,请在此处执行其他操作。*/ }别的{ $data = array('upload_data' => $this->upload->data()); $this->load->view('upload_success', $data); /* 这是一个例子。您可以在成功上传图片后执行任何操作。*/ }

    您可以参考codeigniter手册https://www.codeigniter.com/userguide3/libraries/file_uploading.html

    【讨论】:

    • 是的,没关系,但其他错误,如允许的类型。我想表明除了图片没有上传错误。
    【解决方案2】:
        $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);
           //$rand_str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
             //   $filename=md5(str_shuffle($rand_str.mt_rand()));
            //  $config['file_name']=$filename;
    
                $this->upload->initialize($config);
    
                if ( $this->upload->do_upload('userfile'))
                {
                        //$data = array('upload_data' => $this->upload->data());
                         //  $data["filename"]=$filename;
                        //  $data["ad_id"]=$lid;
    
                        // $data["filename"]=$rand_name;
                      //  $this->Ads_model->insert_image($data);
    
    
    
                }
    

    【讨论】:

      【解决方案3】:

      我解决了我的问题,将 Upload 类更改为没有错误。我评论了包括“没有选择的文件”在内的行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-23
        • 2011-06-08
        • 2012-01-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多