php 文件上传缩略图路径分析类

<?php
//文件上传时分析路径信息
//author:songzhenghe 2014-1-24
//version 0.1
class path_ana {
    private $data_root;
    private $web_root;
    private $http_domain;
    private $file;
    private $r;
     
    public function __construct(){
        //从配置文件中读取
        $this->data_root='E:/wwwroot2/cms/common/upload/';
        $this->web_root ='E:/wwwroot2/cms/';
        $this->http_domain='http://www.cms.com/';
        $this->r=null;
    }
    //$file E:/wwwroot2/cms/common/upload/2013/08/345349534.jpg
    public function init($file){
        $this->file=$file;
        $return=array();
         
        $pathinfo=pathinfo($file);
        // E:/wwwroot2/cms/common/upload/2013/08/
        $return[0]=$pathinfo['dirname'].'/';
        // 345349534.jpg
        $return[1]=$pathinfo['basename'];
        // 345349534
        if(strrpos($return[1],'.')!==false){
            $return[2]=substr($return[1],0,strrpos($return[1],'.'));
        }else{
            $return[2]=$return[1];
        }
        // jpg
        $return[3]=$pathinfo['extension'];
        // 2013/08/345349534.jpg
        $return[4]=$this->str_replace_once($this->data_root,'',$file);
        // 2013/08/
        $return[5]=dirname($return[4]).'/';
        // E:/wwwroot2/cms/common/upload/
        $return[6]=$this->data_root;
        // common/upload/2013/08/345349534.jpg
        $return[7]=$this->str_replace_once($this->web_root,'',$file);
        // common/upload/2013/08/
        $return[8]=dirname($return[7]).'/';
        // E:/wwwroot2/cms/
        $return[9]=$this->web_root;
        // common/upload/
        $return[10]=preg_replace('/'.preg_quote($return[5],'/').'$/i','',$return[8],1);
        $return[11]=$this->http_domain.$return[7];
        // /common/upload/2013/08/345349534.jpg
        $return[12]='/'.$return[7];
        //  E:/wwwroot2/cms/common/upload/2013/08/345349534.jpg
        $return[13]=$this->file;
         
        $this->r=$return;
        return $return;
    }
    private function str_replace_once($needle,$replace,$haystack) {
        $pos=strpos($haystack,$needle);
        if ($pos===false) return $haystack;
        return substr_replace($haystack,$replace,$pos,strlen($needle));
    }
    //
    public function rename_file($prefix='thumb_',$suffix=''){
        if($this->r[3]){
            $new=$this->r[0].$prefix.$this->r[2].$suffix.'.'.$this->r[3];
        }else{
            $new=$this->r[0].$prefix.$this->r[2].$suffix;
        }
        return $new;
    }
     
//
}
echo '<pre>';
$file='E:/wwwroot2/cms/common/upload/2013/08/345349534.jpg';
$path_ana=new path_ana();
$r=$path_ana->init($file);
print_r($r);
 
$file=$path_ana->rename_file();
$r=$path_ana->init($file);
print_r($r);
Array
(
    [0] => E:/wwwroot2/cms/common/upload/2013/08/
    [1] => 345349534.jpg
    [2] => 345349534
    [3] => jpg
    [4] => 2013/08/345349534.jpg
    [5] => 2013/08/
    [6] => E:/wwwroot2/cms/common/upload/
    [7] => common/upload/2013/08/345349534.jpg
    [8] => common/upload/2013/08/
    [9] => E:/wwwroot2/cms/
    [10] => common/upload/
    [11] => http://www.cms.com/common/upload/2013/08/345349534.jpg
    [12] => /common/upload/2013/08/345349534.jpg
    [13] => E:/wwwroot2/cms/common/upload/2013/08/345349534.jpg
)
posted @ 2015-06-17 08:18  宋正河  阅读(231)  评论(0编辑  收藏  举报