【问题标题】:Dynamically Codeigniter Image Upload Rename动态 Codeigniter 图像上传重命名
【发布时间】:2015-07-10 09:53:56
【问题描述】:

我正在使用以下代码使用 codeigniter 上传多个动态图像。

foreach ($_FILES as $key => $value) {   

    $imagePrefix = time();                                  

    if (!$this->upload->do_upload($key))
    {
        echo $errors = $this->upload->display_errors();
        return '';
    }
    else
    {
        $imagename = $imagePrefix.$value['name'];
        $insertImage = $this->db->query("Insert into `tbl_event_imges` (`iEventID`,`vImage`) values ('".$insertId."','".$imagename."')");
    }
}

当我将图像上传到特定文件夹时,效果很好;问题是如果用户上传具有相同名称的图像,那么它会自动重命名图像并上传到文件夹,但我想要通过添加$imagePrefix 来重命名它,我在else 部分添加了代码,然后想上传具有此名称的图像。但这对我不起作用..

有什么建议吗?

【问题讨论】:

  • 您是否遇到任何错误?
  • 不,我正在尝试使用密钥重命名上传文件,但不起作用
  • Codeigniter 上传库仅适用于单个文件上传。

标签: php codeigniter file-upload


【解决方案1】:

您需要为上传功能提供配置首选项,如下所示:

$imagePrefix = time(); 
$imagename = $imagePrefix.$value['name'];

$this->load->library('upload');

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $imagename; // set the name here

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

if (!$this->upload->do_upload($key)){
    ...
}else{
    ...
}

来源:http://www.codeigniter.com/user_guide/libraries/file_uploading.html?highlight=file%20upload#setting-preferences

【讨论】:

    【解决方案2】:

    或者,您可以使用 CI 的重命名功能在文件上传后重命名。

    $image = $this->upload->data();
    $file_path = $image ['file_path'];
    $file = $image ['full_path'];
    $file_ext = $image['file_ext'];
    $final_file_name = time() . $image['file_name'] . $file_ext;
    

    【讨论】:

      【解决方案3】:

      只需添加以下代码即可成功运行并将encrypt_name设置为false

      $path = $_FILES['userfile']['name'];
      $newName = "Add any name".".".pathinfo($path, PATHINFO_EXTENSION);
      $config['file_name'] = $newName; 
      $config['encrypt_name'] = false;
      

      【讨论】:

        猜你喜欢
        • 2016-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-14
        相关资源
        最近更新 更多