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 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入