小的不才,刚学CI不足3个星期,第一次在论坛上发个报道贴,虽然是晚了一点:loveliness:
作为处女贴,先来看看重命名上传
文件的问题,
相信会大家都直接用数组$
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原本就包括扩展名,而如果接收用户输入无扩展名的文件名,上传上去的文件自然没有扩展名,这里再加上已小写处理的扩展名就可以了 至此,重命名上传文件的问题就搞定了!
鄙人不才,无心找茬,代码功力实在羞于现人,但总算发现解决办法,如果能帮到大家忙就最好了:loveliness:
有什么问题,请指出,大家来讨论讨论,如果大家有更好的办法,请指教:handshake