又花了半天时间写了个php上传类解决方法
又花了半天时间写了个php上传类
刚学PHP没多久,为了更好的练习及熟悉PHP,自己花了大半天写了个php上传类,在这里做个笔记,欢迎朋友们对这个类做修改及优化。
- PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->//up_class.php<?phpclass UpFile{ private $inputFile; //文件域名 private $tmpName; //临时文件名 private $tmpPath; //临时文件路径 private $savePath; //保存路径 private $reType; //返回类型 private $upMaxSize; //上传文件大小限制 private $allowFile; //允许上传的文件 private $upFolder; //要上传到的文件夹 private $isReName; //是否要将上传的文件重命名 private $endFileName; //最终保存的文件名 public $fileSize; //文件大小 public $fileType; //文件类型 public $errorInt; //上传失败及错误原因 /** * @param * $inputFile(表单内文件域名称);</br> * $upFolder(保存到服务器的文件夹); * $isRename(对上传文件重命名,值 有 y|n ); * $reType(上传成功后返回的值:n是返回文件名,pn:返回路径和文件名,j:返回js[待扩展]); * $upExt(要上传的文件分类,因为在不同的表单要限制不同的上传文件类型,比如 A表单只能上传图片,B表单只能上伟压缩包); * $maxSize(限制上传的文件大小) * @author:256kb * @2012-5-1 */ public function __construct($inputFile , $upExt = 0 , $reType = 'pn' , $upFolder = 'upload/' , $isRename = 'y' , $maxSize = 10485760){ $this->inputFile = $inputFile; $this->reType = $reType; $this->upMaxSize = $maxSize; $this->allowFile = $upExt; $this->upFolder = $upFolder; $this->isReName = $isRename; //$this->errorInt = -1; } public function upFile(){ $_file_arr = $_FILES[$this->inputFile]; $this->errorInt = $_file_arr['error']; if(is_uploaded_file($_file_arr['tmp_name'])){ if($_file_arr['tmp_name']){ $this->tmpName = $_file_arr['name']; $this->upMaxSize = $_file_arr['size']; $this->fileType = $_file_arr['type']; $this->tmpPath = $_file_arr['tmp_name']; $this->fileSize = $_file_arr['size']; if($this->upMaxSize > $this->upMaxSize){ $this->errorInt = 6 ; //大小超出网站限制 } if(!$this->isAllow()){ $this->errorInt = 8 ; //系统不允许此类型文件 } if($this->isReName=='y'){ $this->savePath = $this->upFolder.$this->getFolder().'/'.$this->getNewName() ; $this->endFileName = $this->getNewName(); }else{ $this->savePath = $this->upFolder.$this->getFolder().'/'.$this->tmpName ; $this->endFileName = $this->tmpName; } //echo $this->errorInt; if(!$this->errorInt >= 1){ move_uploaded_file($this->tmpPath,$this->savePath); } } } } public function getFileUrl(){ switch($this->reType){ case 'n': return $this->endFileName; break; case 'pn': return $this->savePath; break; case 'js': return "<script language='\"javascript\"' type='\"text/javascript\"'>window.parent.LoadAttach('".$this->savePath."');</script>"; break; default: return $this->savePath; } } //获得新文件名 public function getNewName(){ return substr($this->tmpName,1,strrpos($this->tmpName,".")-1).'_'.mktime().'.'.$this->getFileExt(); } public function upStatus(){ //echo $this->errorInt; switch ($this->errorInt){ case 1: return '超过了文件大小php.ini中限制大小'; break; case 2: return '超过了文件大小MAX_FILE_SIZE 选项指定的值'; break; case 3: return '文件只有部分被上传'; break; case 4: return '没有文件被上传'; break; case 5: return '上传文件大小为0'; break; case 6: return '大小超出网站限制'; break; case 7: return '网站内没有指定这种上传类型'; break; case 8: return '系统不允许此类型文件'; case 9: return '创建目录失败!'; break; } } private function isAllow(){ $allow = array( 0 => array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png','application/x-zip-compressed','application/octet-stream'), 1 => array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png'), 2 => array('application/x-zip-compressed','application/octet-stream'), 3 => array('','',''), 4 => array('','','') ); if($this->allowFile > count($allow)-1){ $this->errorInt = 7; //网站内没有指定这种上传类型 }else{ if(in_array($this->fileType, $allow[$this->allowFile])){ return true; }else{ return false; } } } public function getFileExt(){ //获得文件扩展名 return strtolower(substr($this->tmpName,strrpos($this->tmpName,".")+1)); } private function getFolder(){ //获得并自动创建相应文件夹 if(strpos('|rar|zip|7z|iso|','|'.$this->getFileExt().'|')>=0){ $_folder = 'rar'; }elseif(strpos('|gif|jpeg|jpg|png|bmp|pjpeg|psd|','|'.$this->getFileExt().'|')>=0){ $_folder = 'img'; }elseif(strpos('|rm|rmvb|avi|mp4|swf|flv|wmv|','|'.$this->getFileExt().'|')>=0){ $_folder = 'vide'; }elseif(strpos('|doc|txt|xls|mdb||','|'.$this->getFileExt().'|')>=0){ $_folder = 'doc'; }else{ $_folder = 'other'; } if(!file_exists($this->upFolder.$_folder)){ if(!mkdir($this->upFolder.$_folder)){ $this->errorInt = 9; //创建目录失败 } } return $_folder; } }<div class="clear"> </div>

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

许多用户在选择智能手表的时候都会选择的华为的品牌,其中华为GT3pro和GT4都是非常热门的选择,不少用户都很好奇华为GT3pro和GT4有什么区别,下面就就给大家介绍一下二者。华为GT3pro和GT4有什么区别一、外观GT4:46mm和41mm,材质是玻璃表镜+不锈钢机身+高分纤维后壳。GT3pro:46.6mm和42.9mm,材质是蓝宝石玻璃表镜+钛金属机身/陶瓷机身+陶瓷后壳二、健康GT4:采用最新的华为Truseen5.5+算法,结果会更加的精准。GT3pro:多了ECG心电图和血管及安

C语言return的用法有:1、对于返回值类型为void的函数,可以使用return语句来提前结束函数的执行;2、对于返回值类型不为void的函数,return语句的作用是将函数的执行结果返回给调用者;3、提前结束函数的执行,在函数内部,我们可以使用return语句来提前结束函数的执行,即使函数并没有返回值。

为什么截图工具在Windows11上不起作用了解问题的根本原因有助于找到正确的解决方案。以下是截图工具可能无法正常工作的主要原因:对焦助手已打开:这可以防止截图工具打开。应用程序损坏:如果截图工具在启动时崩溃,则可能已损坏。过时的图形驱动程序:不兼容的驱动程序可能会干扰截图工具。来自其他应用程序的干扰:其他正在运行的应用程序可能与截图工具冲突。证书已过期:升级过程中的错误可能会导致此issu简单的解决方案这些适合大多数用户,不需要任何特殊的技术知识。1.更新窗口和Microsoft应用商店应用程

源码:publicclassReturnFinallyDemo{publicstaticvoidmain(String[]args){System.out.println(case1());}publicstaticintcase1(){intx;try{x=1;returnx;}finally{x=3;}}}#输出上述代码的输出可以简单地得出结论:return在finally之前执行,我们来看下字节码层面上发生了什么事情。下面截取case1方法的部分字节码,并且对照源码,将每个指令的含义注释在

在java中,private的意思为“私有的”,是一种访问控制修饰符,用于修饰类、属性和方法。用private修饰的类成员,只能被该类自身的方法访问和修改,而不能被任何其他类(包括该类的子类)访问和引用;因此,private修饰符具有最高的保护级别。

第1部分:初始故障排除步骤检查苹果的系统状态:在深入研究复杂的解决方案之前,让我们从基础知识开始。问题可能不在于您的设备;苹果的服务器可能会关闭。访问Apple的系统状态页面,查看AppStore是否正常工作。如果有问题,您所能做的就是等待Apple修复它。检查您的互联网连接:确保您拥有稳定的互联网连接,因为“无法连接到AppStore”问题有时可归因于连接不良。尝试在Wi-Fi和移动数据之间切换或重置网络设置(“常规”>“重置”>“重置网络设置”>设置)。更新您的iOS版本:

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

private是一种Java访问权限修饰符,它限制函数的可访问性,只允许在定义它的类中访问,包括:在其他类中无法访问该函数。在子类中也无法访问该函数。
