{"id":2215,"date":"2023-03-31T16:25:19","date_gmt":"2023-03-31T08:25:19","guid":{"rendered":"https:\/\/www.tra56.com\/?p=2215"},"modified":"2023-03-31T16:25:19","modified_gmt":"2023-03-31T08:25:19","slug":"2215","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/2215.html","title":{"rendered":"PHP\u5e38\u7528\u7684\u7c7b\u5c01\u88c5\u5c0f\u7ed3\u30104\u4e2a\u5de5\u5177\u7c7b\u3011"},"content":{"rendered":"
\u672c\u6587\u5b9e\u4f8b\u8bb2\u8ff0\u4e86PHP\u5e38\u7528\u7684\u7c7b\u5c01\u88c5\u3002\u5206\u4eab\u7ed9\u5927\u5bb6\u4f9b\u5927\u5bb6\u53c2\u8003\uff0c\u5177\u4f53\u5982\u4e0b\uff1a<\/p>\n
\u8fd94\u4e2a\u7c7b\u5206\u522b\u662fMysql\u7c7b\u3001 \u5206\u9875\u7c7b\u3001\u7f29\u7565\u56fe\u7c7b\u3001\u4e0a\u4f20\u7c7b\u3002<\/p>\n
Mysql\u7c7b<\/strong><\/p>\n \u5206\u9875\u7c7b<\/strong><\/p>\n \u7f29\u7565\u56fe\u7c7b<\/strong><\/p>\n \u4e0a\u4f20\u7c7b<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" \u672c\u6587\u5b9e\u4f8b\u8bb2\u8ff0\u4e86PHP\u5e38\u7528\u7684\u7c7b\u5c01\u88c5\u3002\u5206\u4eab\u7ed9\u5927\u5bb6\u4f9b\u5927\u5bb6\u53c2\u8003\uff0c\u5177\u4f53\u5982\u4e0b\uff1a \u8fd94\u4e2a\u7c7b\u5206\u522b\u662fMysql\u7c7b\u3001 \u5206\u9875\u7c7b\u3001\u7f29\u7565 […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,92],"tags":[],"class_list":["post-2215","post","type-post","status-publish","format-standard","hentry","category-php","category-phpcode"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2215"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=2215"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2215\/revisions"}],"predecessor-version":[{"id":2216,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/2215\/revisions\/2216"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=2215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=2215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=2215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<?php\r\n\/**\r\n * Mysql\u7c7b\r\n *\/\r\nclass Mysql{\r\n private static $link = null;\/\/\u6570\u636e\u5e93\u8fde\u63a5\r\n \/**\r\n * \u79c1\u6709\u7684\u6784\u9020\u65b9\u6cd5\r\n *\/\r\n private function __construct(){}\r\n \/**\r\n * \u8fde\u63a5\u6570\u636e\u5e93\r\n * @return obj \u8d44\u6e90\u5bf9\u8c61\r\n *\/\r\n private static function conn(){\r\n if(self::$link === null){\r\n $cfg = require '.\/config.php';\r\n self::$link = new Mysqli($cfg['host'],$cfg['user'],$cfg['pwd'],$cfg['db']);\r\n self::query("set names ".$cfg['charset']);\/\/\u8bbe\u7f6e\u5b57\u7b26\u96c6\r\n }\r\n return self::$link;\r\n }\r\n \/**\r\n * \u6267\u884c\u4e00\u6761sql\u8bed\u53e5\r\n * @param str $sql \u67e5\u8be2\u8bed\u53e5\r\n * @return obj \u7ed3\u679c\u96c6\u5bf9\u8c61\r\n *\/\r\n public static function query($sql){\r\n return self::conn()->query($sql);\r\n }\r\n \/**\r\n * \u83b7\u53d6\u591a\u884c\u6570\u636e\r\n * @param str $sql \u67e5\u8be2\u8bed\u53e5\r\n * @return arr \u591a\u884c\u6570\u636e\r\n *\/\r\n public static function getAll($sql){\r\n $data = array();\r\n $res = self::query($sql);\r\n while($row = $res->fetch_assoc()){\r\n $data[] = $row;\r\n }\r\n return $data;\r\n }\r\n \/**\r\n * \u83b7\u53d6\u4e00\u884c\u6570\u636e\r\n * @param str $row \u67e5\u8be2\u8bed\u53e5\r\n * @return arr \u5355\u884c\u6570\u636e\r\n *\/\r\n public static function getRow($row){\r\n $res = self::query($sql);\r\n return $res->fetch_assoc();\r\n }\r\n \/**\r\n * \u83b7\u53d6\u5355\u4e2a\u7ed3\u679c\r\n * @param str $sql \u67e5\u8be2\u8bed\u53e5\r\n * @return str \u5355\u4e2a\u7ed3\u679c\r\n *\/\r\n public static function getOne($sql){\r\n $res = self::query($sql);\r\n $data = $res->fetch_row();\r\n return $data[0];\r\n }\r\n \/**\r\n * \u63d2\u5165\/\u66f4\u65b0\u6570\u636e\r\n * @param str $table \u8868\u540d\r\n * @param arr $data \u63d2\u5165\/\u66f4\u65b0\u7684\u6570\u636e\r\n * @param str $act insert\/update\r\n * @param str $where \u66f4\u65b0\u6761\u4ef6\r\n * @return bool \u63d2\u5165\/\u66f4\u65b0\u662f\u5426\u6210\u529f\r\n *\/\r\n public static function exec($table,$data,$act='insert',$where='0'){\r\n \/\/\u63d2\u5165\u64cd\u4f5c\r\n if($act == 'insert'){\r\n $sql = 'insert into '.$table;\r\n $sql .= ' ('.implode(',',array_keys($data)).')';\r\n $sql .= " values ('".implode("','",array_values($data))."')";\r\n }else if($act == 'update'){\r\n $sql = 'update '.$table.' set ';\r\n foreach ($data as $k => $v) {\r\n $sql .= $k.'='."'$v',";\r\n }\r\n $sql = rtrim($sql,',');\r\n $sql .= ' where 1 and '.$where;\r\n }\r\n return self::query($sql);\r\n }\r\n \/**\r\n * \u83b7\u53d6\u6700\u8fd1\u4e00\u6b21\u63d2\u5165\u7684\u4e3b\u952e\u503c\r\n * @return int \u4e3b\u952e\r\n *\/\r\n public static function getLastId(){\r\n return self::conn()->insert_id;\r\n }\r\n \/**\r\n * \u83b7\u53d6\u6700\u8fd1\u4e00\u6b21\u64cd\u4f5c\u5f71\u54cd\u7684\u884c\u6570\r\n * @return int \u5f71\u54cd\u7684\u884c\u6570\r\n *\/\r\n public static function getAffectedRows(){\r\n return self::conn()->affected_rows;\r\n }\r\n \/**\r\n * \u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\r\n * @return bool \u662f\u5426\u5173\u95ed\r\n *\/\r\n public static function close(){\r\n return self::conn()->close();\r\n }\r\n}\r\n?><\/pre>\n
<?php\r\n\/**\r\n * \u5206\u9875\u7c7b\r\n * @author webbc\r\n *\/\r\nclass Page{\r\n private $num;\/\/\u603b\u7684\u6587\u7ae0\u6570\r\n private $cnt;\/\/\u6bcf\u9875\u663e\u793a\u7684\u6587\u7ae0\u6570\r\n private $curr;\/\/\u5f53\u524d\u7684\u9875\u7801\u6570\r\n private $p = 'page';\/\/\u5206\u9875\u53c2\u6570\u540d\r\n private $pageCnt = 5;\/\/\u5206\u680f\u603b\u5171\u663e\u793a\u7684\u9875\u6570\r\n private $firstRow;\/\/\u6bcf\u9875\u7684\u7b2c\u4e00\u884c\u6570\u636e\r\n private $pageIndex = array();\/\/\u5206\u9875\u4fe1\u606f\r\n \/**\r\n * \u6784\u9020\u51fd\u6570\r\n * @param int $num \u603b\u7684\u6587\u7ae0\u6570\r\n * @param int $cnt \u6bcf\u9875\u663e\u793a\u7684\u6587\u7ae0\u6570\r\n *\/\r\n public function __construct($num,$cnt=10){\r\n $this->num = $num;\r\n $this->cnt = $cnt;\r\n $this->curr = empty($_GET[$this->p]) ? 1 : intval($_GET[$this->p]);\r\n $this->curr = $this->curr > 0 ? $this->curr : 1;\r\n $this->firstRow = $this->cnt * ($this->curr - 1);\r\n $this->getPage();\r\n }\r\n \/**\r\n * \u5206\u9875\u65b9\u6cd5\r\n *\/\r\n private function getPage(){\r\n $page = ceil($this->num \/ $this->cnt);\/\/\u603b\u7684\u9875\u6570\r\n $left = max(1,$this->curr - floor($this->pageCnt\/2));\/\/\u8ba1\u7b97\u6700\u5de6\u8fb9\u9875\u7801\r\n $right = min($left + $this->pageCnt - 1 ,$page);\/\/\u8ba1\u7b97\u6700\u53f3\u8fb9\u9875\u7801\r\n $left = max(1,$right - ($this->pageCnt - 1));\/\/\u5f53\u524d\u9875\u7801\u5f80\u53f3\u9760\uff0c\u9700\u8981\u91cd\u65b0\u8ba1\u7b97\u5de6\u8fb9\u9875\u9762\u7684\u503c\r\n for($i=$left;$i<=$right;$i++){\r\n if($i == 1){\r\n $index = '\u7b2c1\u9875';\r\n }else if($i == $page){\r\n $index = '\u6700\u540e\u4e00\u9875';\r\n }else{\r\n $index = '\u7b2c'.$i.'\u9875';\r\n }\r\n $_GET['page'] = $i;\r\n $this->pageIndex[$index] = http_build_query($_GET);\r\n }\r\n }\r\n \/**\r\n * \u8fd4\u56de\u5206\u9875\u4fe1\u606f\u6570\u636e\r\n * @return [type] [description]\r\n *\/\r\n public function show(){\r\n return $this->pageIndex;\r\n }\r\n}\r\n?><\/pre>\n
<?php\r\n\/**\r\n * \u7f29\u7565\u56fe\u7c7b\r\n * @author webbc\r\n *\/\r\nclass Thumb{\r\n private $thumbWidth;\/\/\u7f29\u7565\u56fe\u7684\u5bbd\r\n private $thumbHeight;\/\/\u7f29\u7565\u56fe\u7684\u9ad8\r\n private $thumbPath;\/\/\u7f29\u7565\u56fe\u4fdd\u5b58\u7684\u8def\u5f84\r\n private $sourcePath;\/\/\u539f\u56fe\u7684\u8def\u5f84\r\n private $sourceWidth;\/\/\u539f\u56fe\u7684\u5bbd\u5ea6\r\n private $sourceHeight;\/\/\u539f\u56fe\u7684\u9ad8\u5ea6\r\n private $sourceType;\/\/\u539f\u56fe\u7684\u56fe\u7247\u7c7b\u578b\r\n \/**\r\n * \u6784\u9020\u51fd\u6570\r\n * @param str $sourcePath \u539f\u56fe\u7684\u7edd\u5bf9\u8def\u5f84\r\n * @param integer $thumbWidth \u7f29\u7565\u56fe\u7684\u5bbd\r\n * @param integer $thumbHeight \u7f29\u7565\u56fe\u7684\u9ad8\r\n *\/\r\n public function __construct($sourcePath,$thumbWidth=200,$thumbHeight=200){\r\n \/\/\u83b7\u53d6\u539f\u56fe\u7684\u7edd\u5bf9\u8def\u5f84\r\n $this->sourcePath = $sourcePath;\r\n \/\/\u83b7\u53d6\u7f29\u7565\u56fe\u7684\u5927\u5c0f\r\n $this->thumbWidth = $thumbWidth;\r\n $this->thumbHeight = $thumbHeight;\r\n $this->thumbPath = $this->getThumbPath();\r\n \/\/\u8ba1\u7b97\u5927\u56fe\u7684\u5927\u5c0f\r\n list($this->sourceWidth,$this->sourceHeight,$this->sourceType) = getimagesize($this->sourcePath);\r\n }\r\n \/**\r\n * \u786e\u5b9a\u7f29\u7565\u56fe\u4fdd\u5b58\u7684\u8def\u5f84\r\n * @return [type] [description]\r\n *\/\r\n private function getThumbPath(){\r\n $ext = $this->getExt();\r\n $filename = basename($this->sourcePath,'.'.$ext).'_thumb'.'.'.$ext;\r\n return $thumbPath = __DIR__.'\/'.$filename;\r\n }\r\n \/**\r\n * \u83b7\u53d6\u539f\u56fe\u7684\u6269\u5c55\u540d\r\n * @return str \u6269\u5c55\u540d\r\n *\/\r\n private function getExt(){\r\n return pathinfo($this->sourcePath,PATHINFO_EXTENSION);\r\n }\r\n \/**\r\n * \u68c0\u6d4b\u539f\u56fe\u7684\u6269\u5c55\u540d\u662f\u5426\u5408\u6cd5\uff0c\u5e76\u8fd4\u56de\u76f8\u5e94\u7c7b\u578b\r\n * @return bool\/str \u539f\u56fe\u7684\u7c7b\u578b\r\n *\/\r\n public function getType(){\r\n $typeArr = array(\r\n 1 => 'gif',\r\n 2 => 'jpeg',\r\n 3 => 'png',\r\n 15 => 'wbmp'\r\n );\r\n if(!in_array($this->sourceType, array_keys($typeArr))){\r\n return false;\r\n }\r\n return $typeArr[$this->sourceType];\r\n }\r\n \/**\r\n * \u6309\u7167\u7f29\u7565\u56fe\u5927\u5c0f\uff0c\u8ba1\u7b97\u5927\u56fe\u7684\u7f29\u653e\u6bd4\u4f8b\r\n * @return float \u7f29\u653e\u6bd4\u4f8b\r\n *\/\r\n public function calculateRate(){\r\n return min($this->thumbWidth \/ $this->sourceWidth,$this->thumbHeight \/ $this->sourceHeight);\r\n }\r\n \/**\r\n * \u8ba1\u7b97\u5927\u56fe\u6309\u7167\u7f29\u653e\u6bd4\u4f8b\u540e\uff0c\u6700\u7ec8\u7684\u56fe\u50cf\u5927\u5c0f\r\n * @param float $rate \u7f29\u653e\u6bd4\u4f8b\r\n * @return arr \u7f29\u653e\u540e\u7684\u56fe\u7247\u5927\u5c0f\r\n *\/\r\n public function getImageSizeByRate($rate){\r\n $width = $this->sourceWidth * $rate;\r\n $height = $this->sourceHeight * $rate;\r\n return array('w'=>$width,'h'=>$height);\r\n }\r\n \/**\r\n * \u4fdd\u5b58\u6210\u6587\u4ef6\r\n * @return [type] [description]\r\n *\/\r\n public function saveFile($image){\r\n $method = "image".$this->getType();\r\n $method($image,$this->thumbPath);\r\n }\r\n \/**\r\n * \u8fdb\u884c\u7ed8\u753b\u64cd\u4f5c\r\n * @return [type] [description]\r\n *\/\r\n public function draw(){\r\n if(!($type = $this->getType())){\r\n echo "\u6587\u4ef6\u7c7b\u578b\u4e0d\u652f\u6301";\r\n return ;\r\n }\r\n \/\/\u521b\u5efa\u5927\u56fe\u548c\u5c0f\u56fe\u7684\u753b\u5e03\r\n $method = "imagecreatefrom".$type;\r\n $bigCanvas = $method($this->sourcePath);\r\n $smallCanvas = imagecreatetruecolor($this->thumbWidth, $this->thumbHeight);\r\n \/\/\u521b\u5efa\u767d\u8272\u753b\u7b14\uff0c\u5e76\u7ed9\u5c0f\u56fe\u753b\u5e03\u586b\u5145\u80cc\u666f\r\n $white = imagecolorallocate($smallCanvas, 255, 255, 255);\r\n imagefill($smallCanvas, 0, 0, $white);\r\n \/\/\u8ba1\u7b97\u5927\u56fe\u7684\u7f29\u653e\u6bd4\u4f8b\r\n $rate = $this->calculateRate();\r\n \/\/\u8ba1\u7b97\u5927\u56fe\u7f29\u653e\u540e\u7684\u5927\u5c0f\u4fe1\u606f\r\n $info = $this->getImageSizeByRate($rate);\r\n \/\/\u8fdb\u884c\u7f29\u653e\r\n imagecopyresampled($smallCanvas, $bigCanvas,\r\n ($this->thumbWidth - $info['w']) \/ 2 , ($this->thumbHeight - $info['h']) \/ 2,\r\n 0, 0, $info['w'], $info['h'], $this->sourceWidth, $this->sourceHeight);\r\n \/\/\u4fdd\u5b58\u6210\u6587\u4ef6\r\n $this->saveFile($smallCanvas);\r\n \/\/\u9500\u6bc1\u753b\u5e03\r\n imagedestroy($bigCanvas);\r\n imagedestroy($smallCanvas);\r\n }\r\n}\r\n?><\/pre>\n
<meta charset="utf8"\/>\r\n<?php\r\n\/**\r\n * \u6587\u4ef6\u4e0a\u4f20\u7c7b\r\n * @author webbc\r\n *\/\r\nclass Upload{\r\n private $allowExt = array('gif','jpg','jpeg','bmp','png','swf');\/\/\u9650\u5236\u6587\u4ef6\u4e0a\u4f20\u7684\u540e\u7f00\u540d\r\n private $maxSize = 1;\/\/\u9650\u5236\u6700\u5927\u6587\u4ef6\u4e0a\u4f201M\r\n \/**\r\n * \u83b7\u53d6\u6587\u4ef6\u7684\u4fe1\u606f\r\n * @param str $flag \u4e0a\u4f20\u6587\u4ef6\u7684\u6807\u8bc6\r\n * @return arr \u4e0a\u4f20\u6587\u4ef6\u7684\u4fe1\u606f\u6570\u7ec4\r\n *\/\r\n public function getInfo($flag){\r\n return $_FILES[$flag];\r\n }\r\n \/**\r\n * \u83b7\u53d6\u6587\u4ef6\u7684\u6269\u5c55\u540d\r\n * @param str $filename \u6587\u4ef6\u540d\r\n * @return str \u6587\u4ef6\u6269\u5c55\u540d\r\n *\/\r\n public function getExt($filename){\r\n return pathinfo($filename,PATHINFO_EXTENSION);\r\n }\r\n \/**\r\n * \u68c0\u6d4b\u6587\u4ef6\u6269\u5c55\u540d\u662f\u5426\u5408\u6cd5\r\n * @param str $filename \u6587\u4ef6\u540d\r\n * @return bool \u6587\u4ef6\u6269\u5c55\u540d\u662f\u5426\u5408\u6cd5\r\n *\/\r\n private function checkExt($filename){\r\n $ext = $this->getExt($filename);\r\n return in_array($ext,$this->allowExt);\r\n }\r\n \/**\r\n * \u68c0\u6d4b\u6587\u4ef6\u5927\u5c0f\u662f\u5426\u8d85\u8fc7\u9650\u5236\r\n * @param int size \u6587\u4ef6\u5927\u5c0f\r\n * @return bool \u6587\u4ef6\u5927\u5c0f\u662f\u5426\u8d85\u8fc7\u9650\u5236\r\n *\/\r\n public function checkSize($size){\r\n return $size < $this->maxSize * 1024 * 1024;\r\n }\r\n \/**\r\n * \u968f\u673a\u7684\u6587\u4ef6\u540d\r\n * @param int $len \u968f\u673a\u6587\u4ef6\u540d\u7684\u957f\u5ea6\r\n * @return str \u968f\u673a\u5b57\u7b26\u4e32\r\n *\/\r\n public function randName($len=6){\r\n return substr(str_shuffle('abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ234565789'),0,$len);\r\n }\r\n \/**\r\n * \u521b\u5efa\u6587\u4ef6\u4e0a\u4f20\u5230\u7684\u8def\u5f84\r\n * @return str \u6587\u4ef6\u4e0a\u4f20\u7684\u8def\u5f84\r\n *\/\r\n public function createDir(){\r\n $dir = '.\/upload\/'.date('Y\/m\/d',time());\r\n if(is_dir($dir) || mkdir($dir,0777,true)){\r\n return $dir;\r\n }\r\n }\r\n \/**\r\n * \u6587\u4ef6\u4e0a\u4f20\r\n * @param str $flag \u6587\u4ef6\u4e0a\u4f20\u6807\u8bc6\r\n * @return arr \u6587\u4ef6\u4e0a\u4f20\u4fe1\u606f\r\n *\/\r\n public function uploadFile($flag){\r\n if($_FILES[$flag]['name'] === '' || $_FILES[$flag]['error'] !== 0){\r\n echo "\u6ca1\u6709\u4e0a\u4f20\u6587\u4ef6";\r\n return;\r\n }\r\n $info = $this->getInfo($flag);\r\n if(!$this->checkExt($info['name'])){\r\n echo "\u4e0d\u652f\u6301\u7684\u6587\u4ef6\u7c7b\u578b";\r\n return;\r\n }\r\n if(!$this->checkSize($info['size'])){\r\n echo "\u6587\u4ef6\u5927\u5c0f\u8d85\u8fc7\u9650\u5236";\r\n return;\r\n }\r\n $filename = $this->randName().'.'.$this->getExt($info['name']);\r\n $dir = $this->createDir();\r\n if(!move_uploaded_file($info['tmp_name'], $dir.'\/'.$filename)){\r\n echo "\u6587\u4ef6\u4e0a\u4f20\u5931\u8d25";\r\n }else{\r\n return array('filename'=>$filename,'dir'=>$dir);\r\n }\r\n }\r\n}\r\n?><\/pre>\n