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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?