【问题标题】:Codeigniter multiple upload with different area name and rename the file before uploadedCodeigniter 使用不同的区域名称多次上传并在上传前重命名文件
【发布时间】:2017-11-23 20:23:59
【问题描述】:

我在上传多个不同区域名称的文件时遇到问题,我想在上传之前更改每个区域的文件名。

这是 HTML 表单。

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

这是控制器

    $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.");
    }

注意:图片上传到目录。但我想在上传之前重命名每个文件的文件名,如“userID_profil.jpg”和“userID_top.jpg”

【问题讨论】:

    标签: php codeigniter file-upload


    【解决方案1】:

    您可以在第二个文件上传之前设置$config['file_name']

    $this->upload->initialize($config);
    

    当然,您还需要使用$this-&gt;load-&gt;library('upload', $config)$this-&gt;upload-&gt;initialize($config) 为第一个文件设置它。

    文档:https://www.codeigniter.com/userguide3/libraries/file_uploading.html#setting-preferences

    【讨论】:

      【解决方案2】:

      我解决了。

          $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", ".");
              }
          }
      

      【讨论】:

        猜你喜欢
        • 2013-01-29
        • 1970-01-01
        • 1970-01-01
        • 2014-03-15
        • 2012-12-31
        • 1970-01-01
        • 1970-01-01
        • 2020-03-31
        • 1970-01-01
        相关资源
        最近更新 更多