douyan2002 2017-11-23 12:23
浏览 40
已采纳

Codeigniter使用不同的区域名称进行多次上传,并在上传之前重命名该文件

I have a problem with multiple files upload with different area name and want to change for every area filename before it's upload.

This is HTML form.

<input type="file" placeholder="" name="profilPic"/>
<input type="file" placeholder="" name="topPic"/>

This is controller

    $config['upload_path']          = './uploads/';
    $config['allowed_types']        = 'gif|jpg|png';
    $config['max_size']             = 100;
    $config['max_width']            = 1024;
    $config['max_height']           = 768;
    //$config['file_name']          = $this->session->sersession["id"];
    $this->load->library('upload', $config);
    $profilPic = $this->upload->do_upload('profilPic');
    if (!$profilPic){
        $error = array('error' => $this->upload->display_errors());
        $this->session->set_flashdata("error", "profil pic was not uploaded= ");
    }else{
        $data = array('upload_data' => $this->upload->data());
        $this->session->set_flashdata("success", "profil picture was uploaded.");
    }
    $topPic = $this->upload->do_upload('topPic');
    if (!$topPic){
            $error = array('error' => $this->upload->display_errors());
            $this->session->set_flashdata("error", "top pic was not uploaded" );

    }else{
        $data = array('upload_data' => $this->upload->data());
        $this->session->set_flashdata("success", "this picture was uploaded.");
    }

Note: The pictures are upload to directory. But i want to rename every file file name before uploaded like "userID_profil.jpg" and "userID_top.jpg"

展开全部

  • 写回答

2条回答 默认 最新

  • dongzhen4180 2017-11-23 13:09
    关注

    I solved it.

        $config['upload_path']          = './uploads/';
        $config['allowed_types']        = 'gif|jpg|png';
        $config['max_size']             = 100;
        $config['max_width']            = 1024;
        $config['max_height']           = 768;
        if($_FILES["profilPic"]["name"]){
            $config["file_name"] = $this->session->usersession["id"]."_profil.jpg";
            $this->load->library('upload', $config);
            $profilPic = $this->upload->do_upload('profilPic');
            if (!$profilPic){
                $error = array('error' => $this->upload->display_errors());
                $this->session->set_flashdata("error", ".");
            }else{
                $profilPic = $this->upload->data("file_name");
                $data = array('upload_data' => $this->upload->data());
                $this->session->set_flashdata("success", ".");
            }
        }
    
        if($_FILES["topPic"]["name"]){
            $config["file_name"] = $this->session->usersession["id"]."_top.jpg";
            if($_FILES["profilPic"]["name"]){
                $this->upload->initialize($config);
            }else{
                $this->loadl->library('upload', $config);
            }
            $topPic = $this->upload->do_upload('topPic');
            if (!$topPic){
                $error = array('error' => $this->upload->display_errors());
                $this->session->set_flashdata("error", "" );
            }else{
                $topPic = $this->upload->data("file_name");
                $data = array('upload_data' => $this->upload->data());
                $this->session->set_flashdata("success", ".");
            }
        }
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

悬赏问题

  • ¥100 二维码被拦截如何处理
  • ¥15 怎么解决LogIn.vue中多出来的div
  • ¥15 优博讯dt50巴枪怎么提取镜像
  • ¥30 在CodBlock上用c++语言运行
  • ¥15 求C6748 IIC EEPROM程序固化烧写算法
  • ¥50 关于#php#的问题,请各位专家解答!
  • ¥15 python 3.8.0版本,安装官方库ibm_db遇到问题,提示找不到ibm_db模块。如何解决?
  • ¥15 TMUXHS4412如何防止静电,
  • ¥30 Metashape软件中如何将建模后的图像中的植被与庄稼点云删除
  • ¥20 机械振动学课后习题求解答
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部