在codeigniter

时间:2017-09-18 04:39:09

标签: php codeigniter

我需要在上传后重命名图片名称,因为我的要求是我需要添加id,以便在DB中存储图片名称后可以使用。

我已经完成了将包含和不包含水印的图像上传到不同文件夹的代码,但现在我想更改它的名称,如:

$ imageName = $ imgId。'' .randomstring。''。$ ext;

我尝试使用以下代码,但同一图像存储在带有水印的两个文件夹中。我想将原始图像存储在original_image文件夹中,并在watermark_folder中存储水印图像。请帮我解决这个问题。

$getImgs = $this->photographer_model->get_uploaded_image($result);
                    $getExt = substr($getImgs[0]['img_name'], strrpos($getImgs[0]['img_name'],'.')+0);
                    $randomStr = $this->generate_random_string(20);
                    $fileName  = $result."-".$randomStr."".$getExt;
                    $originalImgPath  = base_url()."assets/original_image/".$fileName;
                    $watermarkImgPath = base_url()."assets/watermark_image/".$fileName;
                    $uploadOrgImg   = 'assets/original_image/'.$fileName;
                    $uploadWaterImg = 'assets/watermark_image/'.$fileName;
                    $updateImg = $this->photographer_model->update_img_path($result, $fileName, $originalImgPath, $watermarkImgPath);
                    if(file_exists('assets/original_image/'.$getImgs[0]['img_name']) || file_exists('assets/watermark_image/'.$getImgs[0]['img_name'])) {
                        unlink('assets/original_image/'.$getImgs[0]['img_name']);
                        unlink('assets/watermark_image/'.$getImgs[0]['img_name']);
                        $config1['upload_path']   = 'assets/original_image/'; 
                        $config1['allowed_types'] = 'jpeg|png|jpg|svg';
                        $config1['file_name']     = $fileName; // set the name here
                        $this->load->library('upload', $config1);
                        $this->upload->initialize($config1);
                        $this->upload->do_upload('image');
                        $config['upload_path']   = 'assets/watermark_image/'; 
                        $config['allowed_types'] = 'jpeg|png|jpg|svg';
                        $config['file_name']     = $fileName; // set the name here
                        $this->load->library('upload', $config);
                        $this->upload->initialize($config);
                        $this->upload->do_upload('image');
                    }

1 个答案:

答案 0 :(得分:0)

使用此

更新您的代码
$config1['upload_path']   = 'assets/original_image/'; 
$config1['allowed_types'] = 'jpeg|png|jpg|svg';
$config1['encrypt_name']  = TRUE;

第二次更新

$config['upload_path']   = 'assets/watermark_image/'; 
$config['allowed_types'] = 'jpeg|png|jpg|svg';
$config['encrypt_name']  = TRUE;