又花了半天时间写了个php上传类解决方法
Jun 13, 2016 am 10:23 AM
gt
private
return
this
又花了半天时间写了个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>
로그인 후 복사
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

인기 기사
R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
2 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
Repo : 팀원을 부활시키는 방법
4 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
헬로 키티 아일랜드 어드벤처 : 거대한 씨앗을 얻는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
스플릿 소설을이기는 데 얼마나 걸립니까?
3 몇 주 전
By DDD
R.E.P.O. 파일 저장 위치 : 어디에 있고 그것을 보호하는 방법은 무엇입니까?
3 몇 주 전
By DDD

인기 기사
R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
2 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
Repo : 팀원을 부활시키는 방법
4 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
헬로 키티 아일랜드 어드벤처 : 거대한 씨앗을 얻는 방법
3 몇 주 전
By 尊渡假赌尊渡假赌尊渡假赌
스플릿 소설을이기는 데 얼마나 걸립니까?
3 몇 주 전
By DDD
R.E.P.O. 파일 저장 위치 : 어디에 있고 그것을 보호하는 방법은 무엇입니까?
3 몇 주 전
By DDD

뜨거운 기사 태그

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제
Gmail 이메일의 로그인 입구는 어디에 있나요?
7312
9


자바 튜토리얼
1624
14


Cakephp 튜토리얼
1346
46


라라벨 튜토리얼
1259
25


PHP 튜토리얼
1207
29



Java에서 return 및 finally 문의 실행 순서는 무엇입니까?

iPhone에서 App Store 오류에 연결할 수 없는 문제를 해결하는 방법
