亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 263
粉絲 3
評論 2
訪問量 133867
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
php四個常用類封裝 (*)
福哥的博客
原創(chuàng)
1067人瀏覽過

mysql類:

<?php/**
 * Mysql類
 */class Mysql{    private static $link = null;//數(shù)據(jù)庫連接

    /**
     * 私有的構(gòu)造方法
     */
    private function __construct(){}    /**
     * 連接數(shù)據(jù)庫
     * @return obj 資源對象
     */
    private static function conn(){        if(self::$link === null){
            $cfg = require './config.php';
            self::$link = new Mysqli($cfg['host'],$cfg['user'],$cfg['pwd'],$cfg['db']);
            self::query("set names ".$cfg['charset']);//設(shè)置字符集
        }        return self::$link;
    }    /**
     * 執(zhí)行一條sql語句
     * @param  str $sql 查詢語句
     * @return obj      結(jié)果集對象
     */
    public static function query($sql){        return self::conn()->query($sql);
    }    /**
     * 獲取多行數(shù)據(jù)
     * @param  str $sql 查詢語句
     * @return arr      多行數(shù)據(jù)
     */     
    public static function getAll($sql){
        $data = array();
        $res = self::query($sql);        while($row = $res->fetch_assoc()){
            $data[] = $row;
        }   
        return $data;
    }    /**
     * 獲取一行數(shù)據(jù)
     * @param  str $row 查詢語句
     * @return arr      單行數(shù)據(jù)
     */ 
    public static function getRow($row){
        $res = self::query($sql);        return $res->fetch_assoc();
    }    /**
     * 獲取單個結(jié)果
     * @param  str $sql 查詢語句
     * @return str      單個結(jié)果
     */     
    public static function getOne($sql){
        $res = self::query($sql);
        $data = $res->fetch_row();        return $data[0];
    }    /**
     * 插入/更新數(shù)據(jù)
     * @param  str $table  表名
     * @param  arr $data  插入/更新的數(shù)據(jù)
     * @param  str $act   insert/update
     * @param  str $where 更新條件
     * @return bool 插入/更新是否成功
     */
    public static function exec($table,$data,$act='insert',$where='0'){        //插入***作
        if($act == 'insert'){
            $sql = 'insert into '.$table;
            $sql .= ' ('.implode(',',array_keys($data)).')';
            $sql .= " values ('".implode("','",array_values($data))."')";
        }else if($act == 'update'){
            $sql = 'update '.$table.' set ';
            foreach ($data as $k => $v) {
                $sql .= $k.'='."'$v',";
            }
            $sql = rtrim($sql,',');
            $sql .= ' where 1 and '.$where;
        }        return self::query($sql);
    }    /**
     * 獲取最近一次插入的主鍵值
     * @return int 主鍵
     */
    public static function getLastId(){        return self::conn()->insert_id;
    }    /**
     * 獲取最近一次***作影響的行數(shù)
     * @return int 影響的行數(shù)
     */
    public static function getAffectedRows(){        return self::conn()->affected_rows;
    }    /**
     * 關(guān)閉數(shù)據(jù)庫連接
     * @return bool 是否關(guān)閉
     */
    public static function close(){        return self::conn()->close();
    }

}
?>

分頁類:

<?php/**
 * 分頁類
 * @author webbc
 */class Page{

    private $num;//總的文章數(shù)
    private $cnt;//每頁顯示的文章數(shù)
    private $curr;//當(dāng)前的頁碼數(shù)
    private $p = 'page';//分頁參數(shù)名
    private $pageCnt = 5;//分欄總共顯示的頁數(shù)
    private $firstRow;//每頁的第一行數(shù)據(jù)
    private $pageIndex = array();//分頁信息

    /**
     * 構(gòu)造函數(shù)
     * @param int $num 總的文章數(shù)
     * @param int $cnt 每頁顯示的文章數(shù)
     */
    public function __construct($num,$cnt=10){
        $this->num = $num;        $this->cnt = $cnt;        $this->curr = empty($_GET[$this->p]) ? 1 : intval($_GET[$this->p]);        $this->curr = $this->curr > 0 ? $this->curr : 1;        $this->firstRow   = $this->cnt * ($this->curr - 1);        $this->getPage();
    }    /**
     * 分頁方法
     */
    private function getPage(){
        $page = ceil($this->num / $this->cnt);//總的頁數(shù)
        $left = max(1,$this->curr - floor($this->pageCnt/2));//計算最左邊頁碼
        $right = min($left + $this->pageCnt - 1 ,$page);//計算最右邊頁碼
        $left = max(1,$right - ($this->pageCnt - 1));//當(dāng)前頁碼往右靠,需要重新計算左邊頁面的值
        for($i=$left;$i<=$right;$i++){            if($i == 1){                $index = '第1頁';
            }else if($i == $page){                $index = '最后一頁';
            }else{                $index = '第'.$i.'頁';
            }            $_GET['page'] = $i;            $this->pageIndex[$index] = http_build_query($_GET);
        }
    }    /**
     * 返回分頁信息數(shù)據(jù)
     * @return [type] [description]
     */
    public function show(){
        return $this->pageIndex;
    }
}?>

縮略圖類:

<?php/**
 * 縮略圖類
 * @author webbc
 */class Thumb{    private $thumbWidth;//縮略圖的寬
    private $thumbHeight;//縮略圖的高
    private $thumbPath;//縮略圖保存的路徑

    private $sourcePath;//原圖的路徑
    private $sourceWidth;//原圖的寬度
    private $sourceHeight;//原圖的高度
    private $sourceType;//原圖的圖***類型


    /**
     * 構(gòu)造函數(shù)
     * @param str  $sourcePath  原圖的絕對路徑
     * @param integer $thumbWidth  縮略圖的寬
     * @param integer $thumbHeight 縮略圖的高
     */
    public function __construct($sourcePath,$thumbWidth=200,$thumbHeight=200){        //獲取原圖的絕對路徑
        $this->sourcePath = $sourcePath;        //獲取縮略圖的大小
        $this->thumbWidth = $thumbWidth;
        $this->thumbHeight = $thumbHeight;
        $this->thumbPath = $this->getThumbPath();        //計算大圖的大小
        list($this->sourceWidth,$this->sourceHeight,$this->sourceType) = getimagesize($this->sourcePath);
    }    /**
     * 確定縮略圖保存的路徑
     * @return [type] [description]
     */
    private function getThumbPath(){
        $ext = $this->getExt();
        $filename = basename($this->sourcePath,'.'.$ext).'_thumb'.'.'.$ext;        return $thumbPath = __DIR__.'/'.$filename;
    }    /**
     * 獲取原圖的擴展名
     * @return str 擴展名
     */
    private function getExt(){        return pathinfo($this->sourcePath,PATHINFO_EXTENSION);
    }    /**
     * 檢測原圖的擴展名是否合法,并返回相應(yīng)類型
     * @return  bool/str 原圖的類型
     */
    public function getType(){
        $typeArr = array(            1 => 'gif',            2 => 'jpeg',            3 => 'png',            15 => 'wbmp'
        );        if(!in_array($this->sourceType, array_keys($typeArr))){            return false;
        }        return $typeArr[$this->sourceType];
    }    /**
     * 按照縮略圖大小,計算大圖的縮放比例
     * @return float 縮放比例
     */
    public function calculateRate(){        return min($this->thumbWidth / $this->sourceWidth,$this->thumbHeight / $this->sourceHeight);
    }    /**
     * 計算大圖按照縮放比例后,最終的圖像大小
     * @param float $rate 縮放比例
     * @return arr 縮放后的圖***大小
     */
    public function getImageSizeByRate($rate){
        $width = $this->sourceWidth * $rate;
        $height = $this->sourceHeight * $rate;        return array('w'=>$width,'h'=>$height);
    }    /**
     * 保存成文件
     * @return [type] [description]
     */
    public function saveFile($image){
        $method = "image".$this->getType();
        $method($image,$this->thumbPath);
    }    /**
     * 進行繪畫***作
     * @return [type] [description]
     */
    public function draw(){        if(!($type = $this->getType())){
            echo "文件類型不支持";            return ;
        }        //創(chuàng)建大圖和小圖的畫布
        $method = "imagecreatefrom".$type;
        $bigCanvas = $method($this->sourcePath);
        $smallCanvas = imagecreatetruecolor($this->thumbWidth, $this->thumbHeight);        //創(chuàng)建白色畫筆,并給小圖畫布填充背景
        $white = imagecolorallocate($smallCanvas, 255, 255, 255);
        imagefill($smallCanvas, 0, 0, $white);        //計算大圖的縮放比例
        $rate = $this->calculateRate();        //計算大圖縮放后的大小信息
        $info = $this->getImageSizeByRate($rate);        //進行縮放
        imagecopyresampled($smallCanvas, $bigCanvas,
            ($this->thumbWidth - $info['w']) / 2 , ($this->thumbHeight - $info['h']) / 2, 
            0, 0, $info['w'], $info['h'], $this->sourceWidth, $this->sourceHeight);        //保存成文件
        $this->saveFile($smallCanvas);        //銷毀畫布
        imagedestroy($bigCanvas);
        imagedestroy($smallCanvas);
    }
}

?>

上傳類:

<meta charset="utf8"/>
<?php/**
 * 文件上傳類
 * @author webbc
 */class Upload{    private $allowExt = array('gif','jpg','jpeg','bmp','png','swf');//限制文件上傳的后綴名
    private $maxSize = 1;//限制最大文件上傳1M

    /**
     * 獲取文件的信息
     * @param  str $flag 上傳文件的標(biāo)識
     * @return arr       上傳文件的信息數(shù)組
     */
    public function getInfo($flag){        return $_FILES[$flag];
    }    /**
     * 獲取文件的擴展名 
     * @param str $filename 文件名
     * @return str 文件擴展名
     */
    public function getExt($filename){        return pathinfo($filename,PATHINFO_EXTENSION);
    }    /**
     * 檢測文件擴展名是否合法
     * @param str $filename 文件名
     * @return bool 文件擴展名是否合法
     */
    private function checkExt($filename){
        $ext = $this->getExt($filename);        return in_array($ext,$this->allowExt);
    }    /**
     * 檢測文件大小是否超過限制
     * @param int size 文件大小
     * @return bool 文件大小是否超過限制
     */
    public function checkSize($size){        return $size < $this->maxSize * 1024 * 1024;
    }    /**
     * 隨機的文件名
     * @param int $len 隨機文件名的長度
     * @return str 隨機字符串
     */
    public function randName($len=6){        return substr(str_shuffle('abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ234565789'),0,$len);
    }    /**
     * 創(chuàng)建文件上傳到的路徑
     * @return str 文件上傳的路徑
     */ 
    public function createDir(){
        $dir = './upload/'.date('Y/m/d',time());        if(is_dir($dir) || mkdir($dir,0777,true)){            return $dir;
        }
    }    /**
     * 文件上傳
     * @param str $flag 文件上傳標(biāo)識
     * @return arr 文件上傳信息
     */
    public function uploadFile($flag){        if($_FILES[$flag]['name'] === '' || $_FILES[$flag]['error'] !== 0){
            echo "沒有上傳文件";            return;
        }
        $info = $this->getInfo($flag);        if(!$this->checkExt($info['name'])){
            echo "不支持的文件類型";            return;
        }        if(!$this->checkSize($info['size'])){
            echo "文件大小超過限制";            return;
        }
        $filename = $this->randName().'.'.$this->getExt($info['name']);
        $dir = $this->createDir();        if(!move_uploaded_file($info['tmp_name'], $dir.'/'.$filename)){
            echo "文件上傳失敗";
        }else{            return array('filename'=>$filename,'dir'=>$dir);
        }
    }

}
?>


本博文版權(quán)歸博主所有,轉(zhuǎn)載請注明地址!如有侵權(quán)、違法,請聯(lián)系admin@php.cn舉報處理!
全部評論 文明上網(wǎng)理性發(fā)言,請遵守新聞評論服務(wù)協(xié)議
0條評論
作者最新博文
關(guān)于我們 免責(zé)申明 意見反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!
關(guān)注服務(wù)號 技術(shù)交流群
PHP中文網(wǎng)訂閱號
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費學(xué)