又花了半天时间写了个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>

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP不是在消亡,而是在不断适应和进化。1)PHP从1994年起经历多次版本迭代,适应新技术趋势。2)目前广泛应用于电子商务、内容管理系统等领域。3)PHP8引入JIT编译器等功能,提升性能和现代化。4)使用OPcache和遵循PSR-12标准可优化性能和代码质量。

PHP的未来将通过适应新技术趋势和引入创新特性来实现:1)适应云计算、容器化和微服务架构,支持Docker和Kubernetes;2)引入JIT编译器和枚举类型,提升性能和数据处理效率;3)持续优化性能和推广最佳实践。

在PHP中,trait适用于需要方法复用但不适合使用继承的情况。1)trait允许在类中复用方法,避免多重继承复杂性。2)使用trait时需注意方法冲突,可通过insteadof和as关键字解决。3)应避免过度使用trait,保持其单一职责,以优化性能和提高代码可维护性。

依赖注入容器(DIC)是一种管理和提供对象依赖关系的工具,用于PHP项目中。DIC的主要好处包括:1.解耦,使组件独立,代码易维护和测试;2.灵活性,易替换或修改依赖关系;3.可测试性,方便注入mock对象进行单元测试。

SplFixedArray在PHP中是一种固定大小的数组,适用于需要高性能和低内存使用量的场景。1)它在创建时需指定大小,避免动态调整带来的开销。2)基于C语言数组,直接操作内存,访问速度快。3)适合大规模数据处理和内存敏感环境,但需谨慎使用,因其大小固定。

PHP通过$\_FILES变量处理文件上传,确保安全性的方法包括:1.检查上传错误,2.验证文件类型和大小,3.防止文件覆盖,4.移动文件到永久存储位置。

JavaScript中处理空值可以使用NullCoalescingOperator(??)和NullCoalescingAssignmentOperator(??=)。1.??返回第一个非null或非undefined的操作数。2.??=将变量赋值为右操作数的值,但前提是该变量为null或undefined。这些操作符简化了代码逻辑,提高了可读性和性能。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

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

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

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