【问题标题】:Codeigniter duplicating image file name with my own upload libraryCodeigniter用我自己的上传库复制图像文件名
【发布时间】:2018-07-15 15:45:44
【问题描述】:

我正在使用 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"] 变量时,它会显示不同的名称...

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

【问题讨论】:

    标签: image codeigniter upload filenames image-uploading


    【解决方案1】:

    您遇到的错误与 CodeIgniter 的工作方式有关:当它加载第一个 $config$this-&gt;load-&gt;library('upload', $config); 时,它不会在您每次初​​始化 UploadImage 类时更改它。因此,您需要加载上传库,而不是初始化$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;
        }
    
    }
    

    【讨论】:

    • 对不起。我评论了我设置 de $config[“file_name”] 的那一行。我知道这很令人困惑,那是因为我改变了很多次,试图找到一种让它发挥作用的方法。 ?
    • 嗯,这就是问题所在。如果没有数组集的那个变量,文件将不会被重命名...
    • 我会试试的。谢谢!
    • 您想在$params 数组中设置文件名(如我在示例中所示)还是希望库像您在注释掉的行中那样生成文件名??
    • 我想它的评论方式会更好。但即使没有评论该行,它也不起作用。有一段时间,我回显了 file_name 变量,它显示了不同的名称。但是文件仍然以相同的名称保存。这就是我感到困惑的原因。我会再试一次,并在这里更新。
    猜你喜欢
    • 2016-11-30
    • 2017-09-02
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 2020-01-23
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    相关资源
    最近更新 更多