• [修补 Patch] 重命名上传文件的解决办法


    [修补 Patch] 重命名上传文件的解决办法

    新版本已经不存在此问题2012年4月12日 10:33:58
    小的不才,刚学CI不足3个星期,第一次在论坛上发个报道贴,虽然是晚了一点
    作为处女贴,先来看看重命名上传文件的问题,
    相信会大家都直接用数组$config['XXX']这种方法来初始化upload的,在system\libraries\Upload.php 的CI_Upload类的初始化方法initialize里面有$default['file_name']这个数组元素,所以很自然就想到设置$config['file_name']这个值就可以重命名文件,但是事实是根本没有作用。
    问题是出在do_upload里面:
    PHP
                    // Set the uploaded data as class variables
                    $this->file_temp = $_FILES[$field]['tmp_name'];
                    //就是下面这行,本来在初始化方法initialize里面设置的$this->file_name值,马上就被下面这行改了原上传文件名
                    $this->file_name = $_FILES[$field]['name'];  
                    $this->file_size = $_FILES[$field]['size'];                
                    $this->file_type = preg_replace("/^(.+?);.*$/", "\1", $_FILES[$field]['type']);
                    $this->file_type = strtolower($this->file_type);
                    $this->file_ext   = strtolower($this->get_extension($_FILES[$field]['name']));
    复制代码
    我的解决办法:
    PHP
                    // Set the uploaded data as class variables
                    $this->file_temp = $_FILES[$field]['tmp_name'];
                   
                    $this->file_size = $_FILES[$field]['size'];                
                    $this->file_type = preg_replace("/^(.+?);.*$/", "\1", $_FILES[$field]['type']);
                    $this->file_type = strtolower($this->file_type);
                    //下面先把扩展名转换为小写,方便后面的使用
                    $this->file_ext   = strtolower($this->get_extension($_FILES[$field]['name']));
                   
                    // 在原基础上添加判断,如果文件没有重命名,就源用原文件名,改文件名的操作就可以实现了
                    if( $this->file_name == "" )        
                            $this->file_name = $_FILES[$field]['name'];
                    else
                            $this->file_name .= $this->file_ext;
                    // file_name原本就包括扩展名,而如果接收用户输入无扩展名的文件名,上传上去的文件自然没有扩展名,这里再加上已小写处理的扩展名就可以了
    复制代码
    至此,重命名上传文件的问题就搞定了!
    鄙人不才,无心找茬,代码功力实在羞于现人,但总算发现解决办法,如果能帮到大家忙就最好了
    有什么问题,请指出,大家来讨论讨论,如果大家有更好的办法,请指教
  • 相关阅读:
    分组声明
    描述项目的典型用户与场景
    用户调研
    10-11-12
    Sprint--5.21
    Cosplay之孩子的妈咪
    作业5.1之5.2
    51nod 1393 1393 0和1相等串
    51nod 1090 3个数和为0(排序+二分)
    51nod 1095 Anigram单词(map的使用)
  • 原文地址:https://www.cnblogs.com/holyes/p/8d23d0c01e2baa1530f476f8353254f4.html
Copyright © 2020-2023  润新知