PHP 文件上传类

来源:互联网 发布:手机幸运转盘软件 编辑:程序博客网 时间:2024/05/14 23:04
<?php/*上传文件类*/class UploadFile(){var $uploadRoot = './uploads/';//存放上传文件的目录var $fileSaved;//记录存放好的上传文件var $types = array('jpeg','jpg','gif','bmp','rar','zip','doc','docx','xls','xlsx','ppt','pptx','txt');/*构造函数str $fileField 上传文件表单中,文件框的name属性,return array*/function UploadFile($fileField,$upLoadRoot=''){$files = $_FILES[$fileField];$fileName = $files['name'];$this->uploadRoot = empty($upLoadRoot)?$this->uploadRoot:$uploadRoot;//检查上传是否出错if($files['error']){?><script language = "javascript">alert("upload error , code : <?=$files['error'];?>");history.back();</script><?php return false;}//检查文件类型if(!$this->_isType($fileName)){ ?><script type="text/javascript">alert("upload file type error.");history.back();</script><? return false;}//创建上传目录if(!$this->_createDir()){?><script type="text/javascript">alert("dir do not create!");history.back();</script><? return false;}//判断是否对上传目录拥有写权限if(!is_writable($this->uploadRoot)){?><script type="text/javascript">alert("can not write to upLoadRoot ");history.back();</script><? return false;}//移动文件到指定地点$this->fileSaved = $this->uploadRoot.$this->_makeSavedFileName($fileName);if(!move_uploaded_file($files["tmp_name",$this->fileSaved)){?><script language = "javascript">alert("move file falled");</script><? return false;}}//检查文件类型  str $fileName 文件名  @ return stringfunction __getFileType($fileName){return end(explode('.',$fileName));}//检查文件类型  str $fileName 文件名  @ return booleanfunction __isType($fileName){$fileType = strtolower(end(explode('.', $fileName)));return in_array($fileType, $this->types);}//存储文件的名字$fileName 文件名function __ _makeSavedFileName($fileName){$names = explode('.',$fileName);return current($names).'_'.time().'.'.end($names);}//检查创建目录function __createDir(){$isFile = file_exists($this->uploadRoot);clearstatcache();if(!isFile && ! is_dir($this->uploadRoot)){if(!@mkdir($this->uploadRoot,0755))return false;}return true;}//删除文件function delFileByName($fileName){if(is_uploaded_file($fileName)){return @unlink($fileName)?true:false;}else{return false;}}}

0 0
原创粉丝点击