ThinkPHP上传类库的Bug修复

最近开发网站用到了ThinkPHP 2.0的上传类库, 用来实现一个图片上传功能。
使用过程中发现,如果开启了子目录创建方式,则会出现一些问题:

public $autoSub = true;

1. 无法生成缩略图, 此问题的修复办法如下, 在 UploadFile.class.php 中找到 line#145

         if ($this->thumb) {
            // 生成图像缩略图
            import("@.ORG.Image");
            $image =  Image::getImageInfo($filename);
            if(false !== $image) {
                //是图像文件生成缩略图
                $thumbWidth		=	explode(',',$this->thumbMaxWidth);
                $thumbHeight		=	explode(',',$this->thumbMaxHeight);
                $thumbPrefix		=	explode(',',$this->thumbPrefix);
                $thumbSuffix = explode(',',$this->thumbSuffix);
                $thumbFile			=	explode(',',$this->thumbFile);
                $thumbPath    =  $this->thumbPath?$this->thumbPath:$file['savepath'];
                for($i=0,$len=count($thumbWidth); $i<$len; $i++) {
                    $thumbname	=	$thumbPath.$thumbPrefix[$i].substr($file['savename'],0,strrpos($file['savename'], '.')).$thumbSuffix[$i].'.'.$file['extension'];
                    Image::thumb($filename,$thumbname,'',$thumbWidth[$i],$thumbHeight[$i],true);
                }

替换成为:

       if ($this->thumb) {
        	$file_savename = $file['savename']; 
        	$sub_path = ''; 
	        if ($this->autoSub) {
	            // 使用子目录保存文件
	            $sub_path = $this->getSubName($file).'/';  
	            $file_savename = str_replace($sub_path,'',$file_savename); 
	        }
        	
            // 生成图像缩略图
            import("@.ORG.Image");
            $image = Image::getImageInfo($filename);
            if (false !== $image) {
                //是图像文件生成缩略图
                $thumbWidth = explode(',',$this->thumbMaxWidth);
                $thumbHeight = explode(',',$this->thumbMaxHeight);
                $thumbPrefix = explode(',',$this->thumbPrefix);
                $thumbSuffix = explode(',',$this->thumbSuffix);
                $thumbFile = explode(',',$this->thumbFile);
                $thumbPath = $this->thumbPath?$this->thumbPath:$file['savepath']; 
                $thumbPath = $thumbPath.$sub_path; 
                
		for ($i=0,$len=count($thumbWidth); $i<$len; $i++) {
                    $thumbname = $thumbPath.$thumbPrefix[$i].substr($file_savename,0,strrpos($file_savename, '.')).$thumbSuffix[$i].'.'.$file['extension']; 
                    Image::thumb($filename,$thumbname,'',$thumbWidth[$i],$thumbHeight[$i],true);
                } 

2. 返回参数中没有包含缩略图的文件名,原始的返回参数如下:

$file['key']          =  $key;
$file['extension']  = $this->getExt($file['name']);
$file['savepath']   = $savePath;
$file['savename']  = $this->getSaveName($file);

savename 参数包含了子目录路径,保存缩略图路径到数据库的时候,还需要写额外的function替换出缩略图的存放路径。
我对此做了一些修改:

$file['key']        = $key; 
$file['extension']  = $this->getExt($file['name']); 
$file['savepath']   = $savePath; 
				
$file['subpath']    = ''; 
$savename = $this->getSaveName($file); 
$file['savename']   = $savename; 
if ($this->autoSub) {
    $file_save_name = explode('/', $savename); 
    $file['subpath']  = $file_save_name[0].'/'; 
    $file['savename'] = $file_save_name[1]; 
} 

返回的参数中多了一个subpath用来存放子目录路径,savename不再包含子目录路径。
如果你的thumbPrefix是’thumb_’, 现在你只需

$thumb_path = $file['subpath'].'thumb_'.$file['savename']

我上传了修改后的UploadFile.class.php文件,你可以随意使用。如果你发现其他问题,欢迎留言讨论。

affiliate_link
Share this Post:
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

One Response to “ThinkPHP上传类库的Bug修复”

  • chen says:

    缩略图放进子目录。但是原文件上传后放了在子目录外!

  • Leave a Reply:

    Name (required):
    Mail (will not be published) (required):
    Website:
    Comment (required):
    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>