Codeigniter 使用我自己的上传库复制图像文件名

Codeigniter duplicating image file name with my own upload library

我正在使用 codeigniter,我有一个 html 表单,其中有 3 张图片要上传。 我正在尝试创建一个库来上传图片,因为我不想重复代码。 问题是,尽管我更改了 "file_name" 参数,但它还是以相同的名称上传的!

这是我的图书馆:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class UploadImage{

    private $fileName = NULL;

    public function __construct($params){
        $this->img = $params["imagem"];
        $this->nome_campo = $params["campo"];
    }

    public function upload(){
        if($this->img != ""){
            $img_name = $this->img["name"]; 
            $img_tmp_name = $this->img["tmp_name"];

            list($width, $height, $type, $attr) = getimagesize($img_tmp_name);

            $name = pathinfo($img_name);

            $ext = pathinfo($img_name, PATHINFO_EXTENSION);

            $diretorio = './assets/frontend/img';

            if (!file_exists($diretorio)) {   
                mkdir($diretorio, 0777, true); 
            }

            $config['file_name'] = md5(microtime().$name["filename"]).".".$ext;

            $this->fileName = $config["file_name"];

            $config["upload_path"] = "./assets/frontend/img";
            $config["allowed_types"] = "jpg|jpeg|gif|png";
            $config["overwrite"] = FALSE;

            $ci = &get_instance();

            $ci->load->library('upload', $config);

            if(!$ci->upload->do_upload($this->nome_campo)) {
                $ci->data['error'] = $ci->upload->display_errors();
                print_r($ci->data['error']);
                return NULL;
            } else {
                if($width > 3000){
                    $config2["source_image"] = "./assets/frontend/img/".$config['file_name'];
                    $config2["create_thumb"] = FALSE;
                    $config2["width"] = 2500;

                    $ci->load->library("image_lib", $config2);

                    if($ci->image_lib->resize()){
                        return TRUE;
                    }else{
                        echo $ci->image_lib->display_errors();
                        return NULL;
                    }
                } else {
                    return TRUE;
                }
            }
        }else{
            return NULL;
        }
    }

    public function getNome(){
        return $this->fileName;
    }
}

这是我在控制器中使用该库的方法:

function salvar(){

    $this->titulo = isset($_POST["titulo"]) ?  $_POST["titulo"] : NULL;

    /* UPLOAD IMAGEM */
    $img1 = isset($_FILES["img1"]) ? $_FILES["img1"] : ""; 
    $img2 = isset($_FILES["img2"]) ? $_FILES["img2"] : ""; 
    $img3 = isset($_FILES["img3"]) ? $_FILES["img3"] : ""; 

    $params1 = $arrayName = array('imagem' => $img1, "campo" => "img1");

    $this->load->library('UploadImage',$params1,"nome_imagem1");
    $this->nome_imagem1->upload();
    echo $this->nome_imagem1->getNome();

    $params2 = $arrayName = array('imagem' => $img2, "campo" => "img2");
    $nome_imagem2 = $this->load->library('UploadImage',$params2,"nome_imagem2");
    $this->nome_imagem2->upload();
    echo $this->nome_imagem2->getNome();

    $params3 = $arrayName = array('imagem' => $img3, "campo" => "img3");
    $nome_imagem3 = $this->load->library('UploadImage',$params3,"nome_imagem3");
    $this->nome_imagem3->upload();
    echo $this->nome_imagem3->getNome();

    $titulo = $_POST["titulo"];
    $descricao = $_POST["descricao"];

    if($this->produtos_model->inserir($titulo,$descricao,$nome_imagem1,$nome_imagem2,$nome_imagem3)){
        echo "Cadastrado com Sucesso!";
    }else{
        echo "Erro ao Cadastrar!";
    }

}   

无论我做什么,它总是以相同的名称保存图像,总是在名称的末尾添加一个“1”。示例:

"0_88434600_1517851942.jpg" 和 “0_88434600_15178519421.jpg”

但是,当打印 $config["file_name"] var 时,它确实显示了不同的名称...

我不知道这里有什么问题。

您遇到的错误与 CodeIgniter 的工作方式有关:当它用 $this->load->library('upload', $config); 加载第一个 $config 时,它不会在您每次初​​始化 [=13] 时更改它=] class。因此你需要加载上传库,然后初始化 $config 数组,然后 reset it.

class UploadImage {

    private $fileName = NULL;
    public $error = NULL;

    public function __construct($params) {
        $this->img = $params["imagem"];
        $this->nome_campo = $params["campo"];
    }

    public function upload() {
        if ($this->img != "") {
            $img_name = $this->img["name"];
            $img_tmp_name = $this->img["tmp_name"];
            list($width, $height, $type, $attr) = getimagesize($img_tmp_name);
            $name = pathinfo($img_name);
            $ext = pathinfo($img_name, PATHINFO_EXTENSION);
            $diretorio = './assets/frontend/img';
            if (!file_exists($diretorio)) {
                mkdir($diretorio, 0777, true);
            }
            $config['file_name'] = md5(microtime() . $name["filename"]) . "." . $ext;
            $this->fileName = $config["file_name"];
            $config["upload_path"] = "./assets/frontend/img";
            $config["allowed_types"] = "jpg|jpeg|gif|png";
            $config["overwrite"] = FALSE;
            // IMPORTANT
            $ci = &get_instance();
            $ci->load->library('upload');
            $ci->upload->initialize($config, true);
            // END IMPORTANT
            if (!$ci->upload->do_upload($this->nome_campo)) {
                $this->error = $ci->upload->display_errors();
                return FALSE;
            } else {
                if ($width > 3000) {
                    $config2["source_image"] = "./assets/frontend/img/" . $config['file_name'];
                    $config2["create_thumb"] = FALSE;
                    $config2["width"] = 2500;
                    $ci->load->library("image_lib", $config2);
                    if ($ci->image_lib->resize()) {
                        return TRUE;
                    } else {
                        $this->error = $ci->image_lib->display_errors();
                        return FALSE;
                    }
                } else {
                    return TRUE;
                }
            }
        } else {
            $this->error = 'No image found.';
            return FALSE;
        }
    }

    public function getNome() {
        return $this->fileName;
    }

}