2019独角兽企业重金招聘Python工程师标准>>>
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st
// +----------------------------------------------------------------------
// $Id$/**+------------------------------------------------------------------------------* 文件上传类+------------------------------------------------------------------------------* @category ORG* @package ORG* @subpackage Net* @author liu21st
class UploadFile
{//类定义开始// 上传文件的最大值public $maxSize &#61; -1;// 是否支持多文件上传public $supportMulti &#61; true;// 允许上传的文件后缀// 留空不作后缀检查public $allowExts &#61; array();// 允许上传的文件类型// 留空不做检查public $allowTypes &#61; array();// 使用对上传图片进行缩略图处理public $thumb &#61; true;// 缩略图最大宽度public $thumbWidth&#61;400;// 缩略图最大高度public $thumbHeight&#61;400;// 缩略图前缀public $thumbPrefix &#61; &#39;thumb_&#39;;public $thumbSuffix &#61; &#39;,&#39;;// 缩略图保存路径public $thumbPath &#61; &#39;&#39;;// 缩略图文件名public $thumbFile &#61;&#39;&#39;;// 是否移除原图public $thumbRemoveOrigin &#61;true;// 启用子目录保存文件public $autoSub &#61; false;// 子目录创建方式 可以使用hash datepublic $subType &#61; &#39;date&#39;;public $dateFormat &#61; &#39;Ym&#39;;public $hashLevel &#61; 1; // hash的目录层次// 上传文件保存路径public $savePath &#61; &#39;&#39;;public $autoCheck &#61; true; // 是否自动检查附件// 存在同名是否覆盖public $uploadReplace &#61; false;// 上传文件命名规则// 例如可以是 time uniqid com_create_guid 等// 必须是一个无需任何参数的函数名 可以使用自定义函数public $saveRule &#61; &#39;uniqid&#39;;// 错误信息private $error &#61; &#39;&#39;;// 上传成功的文件信息private $uploadFileInfo ;/**&#43;----------------------------------------------------------* 架构函数&#43;----------------------------------------------------------* &#64;access public&#43;----------------------------------------------------------*/public function __construct($maxSize&#61;&#39;&#39;,$allowExts&#61;&#39;&#39;,$allowTypes&#61;&#39;&#39;,$savePath&#61;&#39;&#39;,$saveRule&#61;&#39;&#39;){if(!empty($maxSize) && is_numeric($maxSize)) {$this->maxSize &#61; $maxSize;}if(!empty($allowExts)) {if(is_array($allowExts)) {$this->allowExts &#61; array_map(&#39;strtolower&#39;,$allowExts);}else {$this->allowExts &#61; explode(&#39;,&#39;,strtolower($allowExts));}}if(!empty($allowTypes)) {if(is_array($allowTypes)) {$this->allowTypes &#61; array_map(&#39;strtolower&#39;,$allowTypes);}else {$this->allowTypes &#61; explode(&#39;,&#39;,strtolower($allowTypes));}}if(!empty($saveRule)) {$this->saveRule &#61; $saveRule;}$this->savePath .&#61; $savePath;}/**&#43;----------------------------------------------------------* 上传一个文件&#43;----------------------------------------------------------* &#64;access public&#43;----------------------------------------------------------* &#64;param mixed $name 数据* &#64;param string $value 数据表名&#43;----------------------------------------------------------* &#64;return string&#43;----------------------------------------------------------* &#64;throws ThinkExecption&#43;----------------------------------------------------------*/private function save(&$file){$filename &#61; $file[&#39;savepath&#39;].$file[&#39;savename&#39;];if(!$this->uploadReplace && is_file($filename)) {// 不覆盖同名文件$this->error &#61; &#39;文件已经存在&#xff01;&#39;.$filename;return false;}// 如果是图像文件 检测文件格式if( in_array(strtolower($file[&#39;extension&#39;]),array(&#39;gif&#39;,&#39;jpg&#39;,&#39;jpeg&#39;,&#39;bmp&#39;,&#39;png&#39;,&#39;swf&#39;)) && false &#61;&#61;&#61; getimagesize($file[&#39;tmp_name&#39;])) {$this->error &#61; &#39;非法图像文件&#39;;return false;}if(!move_uploaded_file($file[&#39;tmp_name&#39;],$filename)) {$this->error &#61; &#39;文件上传保存错误&#xff01;&#39;;return false;}if($this->thumb && in_array(strtolower($file[&#39;extension&#39;]),array(&#39;gif&#39;,&#39;jpg&#39;,&#39;jpeg&#39;,&#39;bmp&#39;,&#39;png&#39;))) {$image &#61; getimagesize($filename);if(false !&#61;&#61; $image) {//是图像文件生成缩略图$thumbPath &#61; $this->thumbPath?$this->thumbPath:$file[&#39;savepath&#39;];$realFilename &#61; $this->autoSub?basename($file[&#39;savename&#39;]):$file[&#39;savename&#39;];$thumbname&#61;$this->thumbPrefix.substr($realFilename,0,strrpos($realFilename, &#39;.&#39;)).&#39;.&#39;.$file[&#39;extension&#39;]; if(Image::thumb($filename,$thumbPath.$thumbname,&#39;&#39;,$this->thumbWidth,$this->thumbHeight,true)){$file[&#39;thumb&#39;]&#61;$thumbname; } if($this->thumbRemoveOrigin) {// 生成缩略图之后删除原图unlink($filename);} }}return true;}/**&#43;----------------------------------------------------------* 上传所有文件&#43;----------------------------------------------------------* &#64;access public&#43;----------------------------------------------------------* &#64;param string $savePath 上传文件保存路径&#43;----------------------------------------------------------* &#64;return string&#43;----------------------------------------------------------* &#64;throws ThinkExecption&#43;----------------------------------------------------------*/public function upload($savePath &#61;&#39;&#39;){//如果不指定保存文件名&#xff0c;则由系统默认if(empty($savePath))$savePath &#61; $this->savePath;// 检查上传目录if(!is_dir($savePath)) {// 检查目录是否编码后的if(is_dir(base64_decode($savePath))) {$savePath &#61; base64_decode($savePath);}else{// 尝试创建目录if(!mkdir($savePath)){$this->error &#61; &#39;上传目录&#39;.$savePath.&#39;不存在&#39;;return false;}}}else {if(!is_writeable($savePath)) {$this->error &#61; &#39;上传目录&#39;.$savePath.&#39;不可写&#39;;return false;}}$fileInfo &#61; array();$isUpload &#61; false;// 获取上传的文件信息// 对$_FILES数组信息处理$files &#61; $this->dealFiles($_FILES);foreach($files as $key &#61;> $file) {//过滤无效的上传if(!empty($file[&#39;name&#39;])) {//登记上传文件的扩展信息$file[&#39;key&#39;] &#61; $key;$file[&#39;extension&#39;] &#61; $this->getExt($file[&#39;name&#39;]);$file[&#39;savepath&#39;] &#61; $savePath;$file[&#39;savename&#39;] &#61; $this->getSaveName($file);// 自动检查附件if($this->autoCheck) {if(!$this->check($file))return false;}//保存上传文件if(!$this->save($file)) return false;//上传成功后保存文件信息&#xff0c;供其他地方调用unset($file[&#39;tmp_name&#39;],$file[&#39;error&#39;]);$fileInfo[] &#61; $file;$isUpload &#61; true;}}if($isUpload) {$this->uploadFileInfo &#61; $fileInfo;return true;}else {$this->error &#61; &#39;没有选择上传文件&#39;;return false;}}/**&#43;----------------------------------------------------------* 上传单个上传字段中的文件 支持多附件&#43;----------------------------------------------------------* &#64;access public&#43;----------------------------------------------------------* &#64;param array $file 上传文件信息* &#64;param string $savePath 上传文件保存路径&#43;----------------------------------------------------------* &#64;return string&#43;----------------------------------------------------------* &#64;throws ThinkExecption&#43;----------------------------------------------------------*/public function uploadOne($file,$savePath&#61;&#39;&#39;){//如果不指定保存文件名&#xff0c;则由系统默认if(empty($savePath))$savePath &#61; $this->savePath;// 检查上传目录if(!is_dir($savePath)) {// 尝试创建目录if(!mkdir($savePath)){$this->error &#61; &#39;上传目录&#39;.$savePath.&#39;不存在&#39;;return false;}}else {if(!is_writeable($savePath)) {$this->error &#61; &#39;上传目录&#39;.$savePath.&#39;不可写&#39;;return false;}}//过滤无效的上传if(!empty($file[&#39;name&#39;])) {$fileArray &#61; array();if(is_array($file[&#39;name&#39;])) {$keys &#61; array_keys($file);$count &#61; count($file[&#39;name&#39;]);for ($i&#61;0; $i<$count; $i&#43;&#43;) {foreach ($keys as $key)$fileArray[$i][$key] &#61; $file[$key][$i];}}else{$fileArray[] &#61; $file;}$info &#61; array();foreach ($fileArray as $key&#61;>$file){//登记上传文件的扩展信息$file[&#39;extension&#39;] &#61; $this->getExt($file[&#39;name&#39;]);$file[&#39;savepath&#39;] &#61; $savePath;$file[&#39;savename&#39;] &#61; $this->getSaveName($file);// 自动检查附件if($this->autoCheck) {if(!$this->check($file))return false;}//保存上传文件if(!$this->save($file)) return false;unset($file[&#39;tmp_name&#39;],$file[&#39;error&#39;]);$info[] &#61; $file;$isUpload&#61;true;}if($isUpload) {$this->uploadFileInfo &#61; $info;return true;// 返回上传的文件信息}}else {$this->error &#61; &#39;没有选择上传文件&#39;;return false;}}/**&#43;----------------------------------------------------------* 转换上传文件数组变量为正确的方式&#43;----------------------------------------------------------* &#64;access private&#43;----------------------------------------------------------* &#64;param array $files 上传的文件变量&#43;----------------------------------------------------------* &#64;return array&#43;----------------------------------------------------------*/private function dealFiles($files) {$fileArray &#61; array();$n &#61; 0;foreach ($files as $file){if(is_array($file[&#39;name&#39;])) {$keys &#61; array_keys($file);$count &#61; count($file[&#39;name&#39;]);for ($i&#61;0; $i<$count; $i&#43;&#43;) {foreach ($keys as $key)$fileArray[$n][$key] &#61; $file[$key][$i];$n&#43;&#43;;}}else{$fileArray[$n] &#61; $file;$n&#43;&#43;;}}return $fileArray;}/**&#43;----------------------------------------------------------* 获取错误代码信息&#43;----------------------------------------------------------* &#64;access public&#43;----------------------------------------------------------* &#64;param string $errorNo 错误号码&#43;----------------------------------------------------------* &#64;return void&#43;----------------------------------------------------------* &#64;throws ThinkExecption&#43;----------------------------------------------------------*/protected function error($errorNo){switch($errorNo) {case 1:$this->error &#61; &#39;上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值&#39;;break;case 2:$this->error &#61; &#39;上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值&#39;;break;case 3:$this->error &#61; &#39;文件只有部分被上传&#39;;break;case 4:$this->error &#61; &#39;没有文件被上传&#39;;break;case 6:$this->error &#61; &#39;找不到临时文件夹&#39;;break;case 7:$this->error &#61; &#39;文件写入失败&#39;;break;default:$this->error &#61; &#39;未知上传错误&#xff01;&#39;;}return ;}/**&#43;----------------------------------------------------------* 根据上传文件命名规则取得保存文件名&#43;----------------------------------------------------------* &#64;access private&#43;----------------------------------------------------------* &#64;param string $filename 数据&#43;----------------------------------------------------------* &#64;return string&#43;----------------------------------------------------------*/private function getSaveName($filename){$rule &#61; $this->saveRule;if(empty($rule)) {//没有定义命名规则&#xff0c;则保持文件名不变$saveName &#61; $filename[&#39;name&#39;];}else {if(function_exists($rule)) {//使用函数生成一个唯一文件标识号$saveName &#61; $rule().".".$filename[&#39;extension&#39;];}else {//使用给定的文件名作为标识号$saveName &#61; $rule.".".$filename[&#39;extension&#39;];}}if($this->autoSub) {// 使用子目录保存文件$filename[&#39;savename&#39;] &#61; $saveName;$saveName &#61; $this->getSubName($filename).&#39;/&#39;.$saveName;}return $saveName;}/**&#43;----------------------------------------------------------* 获取子目录的名称&#43;----------------------------------------------------------* &#64;access private&#43;----------------------------------------------------------* &#64;param array $file 上传的文件信息&#43;----------------------------------------------------------* &#64;return string&#43;----------------------------------------------------------*/private function getSubName($file){switch($this->subType) {case &#39;date&#39;:$dir &#61; date($this->dateFormat,time());break;case &#39;hash&#39;:default:$name &#61; md5($file[&#39;savename&#39;]);$dir &#61; &#39;&#39;;for($i&#61;0;$i<$this->hashLevel;$i&#43;&#43;) {$dir .&#61; $name{$i}.&#39;/&#39;;}break;}if(!is_dir($file[&#39;savepath&#39;].$dir)) {mkdir($file[&#39;savepath&#39;].$dir);}return $dir;}/**&#43;----------------------------------------------------------* 检查上传的文件&#43;----------------------------------------------------------* &#64;access private&#43;----------------------------------------------------------* &#64;param array $file 文件信息&#43;----------------------------------------------------------* &#64;return boolean&#43;----------------------------------------------------------*/private function check($file) {if($file[&#39;error&#39;]!&#61;&#61; 0) {//文件上传失败//捕获错误代码$this->error($file[&#39;error&#39;]);return false;}//文件上传成功&#xff0c;进行自定义规则检查//检查文件大小if(!$this->checkSize($file[&#39;size&#39;])) {$this->error &#61; &#39;上传文件大小不符&#xff01;&#39;;return false;}//检查文件Mime类型if(!$this->checkType($file[&#39;type&#39;])) {$this->error &#61; &#39;上传文件MIME类型不允许&#xff01;&#39;;return false;}//检查文件类型if(!$this->checkExt($file[&#39;extension&#39;])) {$this->error &#61;&#39;上传文件类型不允许&#39;;return false;}//检查是否合法上传if(!$this->checkUpload($file[&#39;tmp_name&#39;])) {$this->error &#61; &#39;非法上传文件&#xff01;&#39;;return false;}return true;}/**&#43;----------------------------------------------------------* 检查上传的文件类型是否合法&#43;----------------------------------------------------------* &#64;access private&#43;----------------------------------------------------------* &#64;param string $type 数据&#43;----------------------------------------------------------* &#64;return boolean&#43;----------------------------------------------------------*/private function checkType($type){if(!empty($this->allowTypes))return in_array(strtolower($type),$this->allowTypes);return true;}/**&#43;----------------------------------------------------------* 检查上传的文件后缀是否合法&#43;----------------------------------------------------------* &#64;access private&#43;----------------------------------------------------------* &#64;param string $ext 后缀名&#43;----------------------------------------------------------* &#64;return boolean&#43;----------------------------------------------------------*/private function checkExt($ext){if(!empty($this->allowExts))return in_array(strtolower($ext),$this->allowExts,true);return true;}/**&#43;----------------------------------------------------------* 检查文件大小是否合法&#43;----------------------------------------------------------* &#64;access private&#43;----------------------------------------------------------* &#64;param integer $size 数据&#43;----------------------------------------------------------* &#64;return boolean&#43;----------------------------------------------------------*/private function checkSize($size){return !($size > $this->maxSize) || (-1 &#61;&#61; $this->maxSize);}/**&#43;----------------------------------------------------------* 检查文件是否非法提交&#43;----------------------------------------------------------* &#64;access private&#43;----------------------------------------------------------* &#64;param string $filename 文件名&#43;----------------------------------------------------------* &#64;return boolean&#43;----------------------------------------------------------*/private function checkUpload($filename){return is_uploaded_file($filename);}/**&#43;----------------------------------------------------------* 取得上传文件的后缀&#43;----------------------------------------------------------* &#64;access private&#43;----------------------------------------------------------* &#64;param string $filename 文件名&#43;----------------------------------------------------------* &#64;return boolean&#43;----------------------------------------------------------*/private function getExt($filename){$pathinfo &#61; pathinfo($filename);return $pathinfo[&#39;extension&#39;];}/**&#43;----------------------------------------------------------* 取得上传文件的信息&#43;----------------------------------------------------------* &#64;access public&#43;----------------------------------------------------------* &#64;return array&#43;----------------------------------------------------------*/public function getUploadFileInfo(){return $this->uploadFileInfo;}/**&#43;----------------------------------------------------------* 取得最后一次错误信息&#43;----------------------------------------------------------* &#64;access public&#43;----------------------------------------------------------* &#64;return string&#43;----------------------------------------------------------*/public function getErrorMsg(){return $this->error;}}//类定义结束
?>// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// &#43;----------------------------------------------------------------------
// | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
// &#43;----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// &#43;----------------------------------------------------------------------
// | Author: liu21st
// &#43;----------------------------------------------------------------------
// $Id$/**&#43;------------------------------------------------------------------------------* 图像操作类库&#43;------------------------------------------------------------------------------* &#64;category ORG* &#64;package ORG* &#64;subpackage Util* &#64;author liu21st
class Image{//类定义开始/**&#43;----------------------------------------------------------* 取得图像信息*&#43;----------------------------------------------------------* &#64;static* &#64;access public&#43;----------------------------------------------------------* &#64;param string $image 图像文件名&#43;----------------------------------------------------------* &#64;return mixed&#43;----------------------------------------------------------*/static function getImageInfo($img) {$imageInfo &#61; getimagesize($img);if ($imageInfo !&#61;&#61; false) {$imageType &#61; strtolower(substr(image_type_to_extension($imageInfo[2]), 1));$imageSize &#61; filesize($img);$info &#61; array("width" &#61;> $imageInfo[0],"height" &#61;> $imageInfo[1],"type" &#61;> $imageType,"size" &#61;> $imageSize,"mime" &#61;> $imageInfo[&#39;mime&#39;]);return $info;} else {return false;}}/**&#43;----------------------------------------------------------* 为图片添加水印&#43;----------------------------------------------------------* &#64;static public&#43;----------------------------------------------------------* &#64;param string $source 原文件名* &#64;param string $water 水印图片* &#64;param string $$savename 添加水印后的图片名* &#64;param string $alpha 水印的透明度&#43;----------------------------------------------------------* &#64;return string&#43;----------------------------------------------------------* &#64;throws ThinkExecption&#43;----------------------------------------------------------*/static public function water($source, $water, $savename&#61;null, $alpha&#61;80) {//检查文件是否存在if (!file_exists($source) || !file_exists($water))return false;//图片信息$sInfo &#61; self::getImageInfo($source);$wInfo &#61; self::getImageInfo($water); //如果图片小于水印图片&#xff0c;不生成图片if ($sInfo["width"] < $wInfo["width"] || $sInfo[&#39;height&#39;] < $wInfo[&#39;height&#39;])return false;//建立图像$sCreateFun &#61; "imagecreatefrom" . $sInfo[&#39;type&#39;];$sImage &#61; $sCreateFun($source);$wCreateFun &#61; "imagecreatefrom" . $wInfo[&#39;type&#39;];$wImage &#61; $wCreateFun($water);//设定图像的混色模式imagealphablending($wImage, true);//图像位置,默认为右下角右对齐$posY &#61; $sInfo["height"] - $wInfo["height"];$posX &#61; $sInfo["width"] - $wInfo["width"];//生成混合图像imagecopymerge($sImage, $wImage, $posX, $posY, 0, 0, $wInfo[&#39;width&#39;], $wInfo[&#39;height&#39;], $alpha);//输出图像$ImageFun &#61; &#39;Image&#39; . $sInfo[&#39;type&#39;];//如果没有给出保存文件名&#xff0c;默认为原图像名if (!$savename) {$savename &#61; $source;&#64;unlink($source);}//保存图像$ImageFun($sImage, $savename);imagedestroy($sImage);return true;}/**&#43;----------------------------------------------------------* 生成缩略图&#43;----------------------------------------------------------* &#64;static* &#64;access public&#43;----------------------------------------------------------* &#64;param string $image 原图* &#64;param string $type 图像格式* &#64;param string $thumbname 缩略图文件名* &#64;param string $maxWidth 宽度* &#64;param string $maxHeight 高度* &#64;param string $position 缩略图保存目录* &#64;param boolean $interlace 启用隔行扫描&#43;----------------------------------------------------------* &#64;return void&#43;----------------------------------------------------------*/static function thumb($image, $thumbname, $type&#61;&#39;&#39;, $maxWidth&#61;200, $maxHeight&#61;50, $interlace&#61;true) {// 获取原图信息$info &#61; Image::getImageInfo($image);if ($info !&#61;&#61; false) {$srcWidth &#61; $info[&#39;width&#39;];$srcHeight &#61; $info[&#39;height&#39;];$type &#61; empty($type) ? $info[&#39;type&#39;] : $type;$type &#61; strtolower($type);$interlace &#61; $interlace ? 1 : 0;unset($info);$scale &#61; min($maxWidth / $srcWidth, $maxHeight / $srcHeight); // 计算缩放比例if ($scale >&#61; 1) {// 超过原图大小不再缩略$width &#61; $srcWidth;$height &#61; $srcHeight;} else {// 缩略图尺寸$width &#61; (int) ($srcWidth * $scale);$height &#61; (int) ($srcHeight * $scale);}// 载入原图$createFun &#61; &#39;ImageCreateFrom&#39; . ($type &#61;&#61; &#39;jpg&#39; ? &#39;jpeg&#39; : $type);$srcImg &#61; $createFun($image);//创建缩略图if ($type !&#61; &#39;gif&#39; && function_exists(&#39;imagecreatetruecolor&#39;))$thumbImg &#61; imagecreatetruecolor($width, $height);else$thumbImg &#61; imagecreate($width, $height);// 复制图片if (function_exists("ImageCopyResampled"))imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);elseimagecopyresized($thumbImg, $srcImg, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight);if (&#39;gif&#39; &#61;&#61; $type || &#39;png&#39; &#61;&#61; $type) {//imagealphablending($thumbImg, false);//取消默认的混色模式//imagesavealpha($thumbImg,true);//设定保存完整的 alpha 通道信息$background_color &#61; imagecolorallocate($thumbImg, 0, 255, 0); // 指派一个绿色imagecolortransparent($thumbImg, $background_color); // 设置为透明色&#xff0c;若注释掉该行则输出绿色的图}// 对jpeg图形设置隔行扫描if (&#39;jpg&#39; &#61;&#61; $type || &#39;jpeg&#39; &#61;&#61; $type)imageinterlace($thumbImg, $interlace);//$gray&#61;ImageColorAllocate($thumbImg,255,0,0);//ImageString($thumbImg,2,5,5,"ThinkPHP",$gray);// 生成图片$imageFun &#61; &#39;image&#39; . ($type &#61;&#61; &#39;jpg&#39; ? &#39;jpeg&#39; : $type);$imageFun($thumbImg, $thumbname);imagedestroy($thumbImg);imagedestroy($srcImg);return $thumbname;}return false;}}//类定义结束
?>测试文件
header("Content-type:text/html;charset&#61;utf-8");
require "UploadFile.class.php";
require "Image.class.php";
$upload&#61;new UploadFile();
if(!$upload->uploadOne($_FILES[&#39;name&#39;],ROOT.&#39;upload/&#39;)){$upload->getErrorMsg();
}else{$list&#61;$upload->getUploadFileInfo();$info&#61;array();foreach ($list as $val){$info&#61;$val;} $water&#61;ROOT.&#39;data/logo.png&#39;; $source&#61;$info[&#39;savepath&#39;].$info[&#39;thumb&#39;];$savename&#61;&#39;water&#39;.$info[&#39;savename&#39;];if(Image::water($source, $water)){}var_dump($info);
}?>