繁体   English   中英

Codeigniter上传图片选择

[英]Codeigniter Upload Image Choice

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

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

您步入正轨。 只需删除或评论此行

$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());
                /*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.*/
            }

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

    $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);



            }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM