douyan2002 2017-11-23 20: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 21: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条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码