diff --git a/ex.php b/ex.php new file mode 100644 index 0000000..a12f0ae --- /dev/null +++ b/ex.php @@ -0,0 +1,242 @@ +setOption($key, $val); + } + return $this; + } + + /** + * 调用该方法上传文件 + * @param string $fileFile 上传文件的表单名称 + * @return bool 如果上传成功返回数true + */ + + function upload($fileField="files_0") { + $return = true; + /* 检查文件路径是滞合法 */ + if( !$this->checkFilePath() ) { + $this->errorMess = $this->getError(); + return false; + } + // var_dump($_FILES['files_0']['tmp_name']); + /* 将文件上传的信息取出赋给变量 */ + $name = $_FILES[$fileField]['name']; + $tmp_name = $_FILES[$fileField]['tmp_name']; + $size = $_FILES[$fileField]['size']; + $error = $_FILES[$fileField]['error']; + + /* 如果是多个文件上传则$file["name"]会是一个数组 */ + if(is_Array($name)){ + $errors=array(); + /*多个文件上传则循环处理 , 这个循环只有检查上传文件的作用,并没有真正上传 */ + for($i = 0; $i < count($name); $i++){ + /*设置文件信息 */ + if($this->setFiles($name[$i],$tmp_name[$i],$size[$i],$error[$i] )) { + if(!$this->checkFileSize() || !$this->checkFileType()){ + $errors[] = $this->getError(); + $return=false; + } + }else{ + $errors[] = $this->getError(); + $return=false; + } + /* 如果有问题,则重新初使化属性 */ + if(!$return) + $this->setFiles(); + } + + if($return){ + /* 存放所有上传后文件名的变量数组 */ + $fileNames = array(); + /* 如果上传的多个文件都是合法的,则通过销魂循环向服务器上传文件 */ + for($i = 0; $i < count($name); $i++){ + if($this->setFiles($name[$i], $tmp_name[$i], $size[$i], $error[$i] )) { + $this->setNewFileName(); + if(!$this->copyFile()){ + $errors[] = $this->getError(); + $return = false; + } + $fileNames[] = $this->newFileName; + } + } + $this->newFileName = $fileNames; + } + $this->errorMess = $errors; + return $return; + /*上传单个文件处理方法*/ + } else { + /* 设置文件信息 */ + if($this->setFiles($name,$tmp_name,$size,$error)) { + /* 上传之前先检查一下大小和类型 */ + if($this->checkFileSize() && $this->checkFileType()){ + /* 为上传文件设置新文件名 */ + $this->setNewFileName(); + /* 上传文件 返回0为成功, 小于0都为错误 */ + if($this->copyFile()){ + return true; + }else{ + $return=false; + } + }else{ + $return=false; + } + } else { + $return=false; + } + //如果$return为false, 则出错,将错误信息保存在属性errorMess中 + if(!$return) + $this->errorMess=$this->getError(); + + return $return; + } + } + + /** + * 获取上传后的文件名称 + * @param void 没有参数 + * @return string 上传后,新文件的名称, 如果是多文件上传返回数组 + */ + public function getFileName(){ + return $this->newFileName; + } + + /** + * 上传失败后,调用该方法则返回,上传出错信息 + * @param void 没有参数 + * @return string 返回上传文件出错的信息报告,如果是多文件上传返回数组 + */ + public function getErrorMsg(){ + return $this->errorMess; + } + + /* 设置上传出错信息 */ + private function getError() { + $str = "上传文件{$this->originName}时出错 : "; + switch ($this->errorNum) { + case 4: $str .= "没有文件被上传"; break; + case 3: $str .= "文件只有部分被上传"; break; + case 2: $str .= "上传文件的大小超过了HTML表单中MAX_FILE_SIZE选项指定的值"; break; + case 1: $str .= "上传的文件超过了php.ini中upload_max_filesize选项限制的值"; break; + case -1: $str .= "未允许类型"; break; + case -2: $str .= "文件过大,上传的文件不能超过{$this->maxsize}个字节"; break; + case -3: $str .= "上传失败"; break; + case -4: $str .= "建立存放上传文件目录失败,请重新指定上传目录"; break; + case -5: $str .= "必须指定上传文件的路径"; break; + default: $str .= "未知错误"; + } + return $str.'
'; + } + + /* 设置和$_FILES有关的内容 */ + private function setFiles($name="", $tmp_name="", $size=0, $error=0) { + $this->setOption('errorNum', $error); + if($error) + return false; + $this->setOption('originName', $name); + $this->setOption('tmpFileName',$tmp_name); + $aryStr = explode(".", $name); + $this->setOption('fileType', strtolower($aryStr[count($aryStr)-1])); + $this->setOption('fileSize', $size); + return true; + } + + /* 为单个成员属性设置值 */ + private function setOption($key, $val) { + $this->$key = $val; + } + + /* 设置上传后的文件名称 */ + private function setNewFileName() { + if ($this->israndname) { + $this->setOption('newFileName', $this->proRandName()); + } else{ + $this->setOption('newFileName', $this->originName); + } + } + + /* 检查上传的文件是否是合法的类型 */ + private function checkFileType() { + if (in_array(strtolower($this->fileType), $this->allowtype)) { + return true; + }else { + $this->setOption('errorNum', -1); + return false; + } + } + + /* 检查上传的文件是否是允许的大小 */ + private function checkFileSize() { + if ($this->fileSize > $this->maxsize) { + $this->setOption('errorNum', -2); + return false; + }else{ + return true; + } + } + + /* 检查是否有存放上传文件的目录 */ + private function checkFilePath() { + if(empty($this->path)){ + $this->setOption('errorNum', -5); + return false; + } + if (!file_exists($this->path) || !is_writable($this->path)) { + if (!@mkdir($this->path, 0755)) { + $this->setOption('errorNum', -4); + return false; + } + } + return true; + } + + /* 设置随机文件名 */ + private function proRandName() { + $fileName = date('YmdHis')."_".rand(100,999); + return $fileName.'.'.$this->fileType; + } + + /* 复制上传文件到指定的位置 */ + private function copyFile() { + if(!$this->errorNum) { + $path = rtrim($this->path, '/').'/'; + $path .= $this->newFileName; + var_dump($this->tmpFileName); + var_dump($path); + if (@move_uploaded_file($this->tmpFileName, $path)) { + return true; + }else{ + $this->setOption('errorNum', -3); + return false; + } + } else { + return false; + } + } + } \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..3be0ffd --- /dev/null +++ b/index.html @@ -0,0 +1,12 @@ + + + + 首页---MY + + + 这是首页 + + + \ No newline at end of file diff --git a/myUpload.html b/myUpload.html new file mode 100644 index 0000000..313d09e --- /dev/null +++ b/myUpload.html @@ -0,0 +1,96 @@ + + + + + + myUpload + + + + +
+ + + + + + +
+ +
+
+ + + + + diff --git a/myUpload.js b/myUpload.js new file mode 100644 index 0000000..9f4fb27 --- /dev/null +++ b/myUpload.js @@ -0,0 +1,88 @@ +/** + * [myUpload description] + * @param {[type]} options [description] + * options.element 上传标签对象 [必填] + * options.url 上传地址 [必填] + * options.success 上传成功回调 [选填] + * options.error 上传失败回调 [选填] + * options.progress 上传进度回调 [选填] + */ +function myUpload(options) { + if (!(this instanceof myUpload)) { + return new myUpload(options); + } + this.init(options); +} + +myUpload.prototype = { + init: function(options) { + if (!this.validateOptions(options)) { + throw '参数错误'; + } + + this.xhr = new XMLHttpRequest(); + this.formdata = new FormData(); + this.element = options.element; + this.url = options.url; + this.options = options; + + this.initFile(); + + }, + + initFile: function() { + var filename = 'files_'; + for (var i = 0; i < this.element.files.length; i++) { + this.formdata.append(filename + i, this.element.files[0]); + } + + this.send(); + }, + + validateOptions: function(options) { + if (!options.element || !options.url) { + return false; + } else { + return true; + } + }, + + send: function() { + var self = this; + self.xhr.upload.addEventListener("progress", function(e) { + self.progress.call(self, e); + }, false); + self.addEventListener("load", function(e) { + self.success.call(self, e); + }); + self.addEventListener("error", function(e) { + self.error.call(self, e); + }); + + self.xhr.open("POST", this.url); + self.xhr.send(this.formdata); + }, + + addEventListener: function(type, callback) { + this.xhr.addEventListener(type, callback, false); + }, + + success: function(e) { + var response = e.target.response; + var results = JSON.parse(response); + this.options.success(results); + }, + + error: function(e) { + var response = e.target.response; + var results = JSON.parse(response); + this.options.success(results); + }, + + progress: function(e) { + // var response = e.target.response; + // var results = JSON.parse(response); + // this.options.success(results); + } + +} diff --git a/myUpload.php b/myUpload.php new file mode 100644 index 0000000..4f1381b --- /dev/null +++ b/myUpload.php @@ -0,0 +1,13 @@ + upload() ; + if(count($results)){ + echo json_encode(array('data' => $results , 'info' => '' , 'status' => '' )) ; + }else{ + echo json_encode(array('data' => '' , 'info' => '' , 'status' => '' )) ; + } diff --git a/upload.class.php b/upload.class.php new file mode 100644 index 0000000..5cd9332 --- /dev/null +++ b/upload.class.php @@ -0,0 +1,54 @@ + $v){ + $this -> initFile( $v ); + $path = $this -> savefile(); + array_push( $results , $path ); + } + return $results; + } + + function initFile($file){ + $this -> originName = $file['name']; + $this -> tmpFileName = $file['tmp_name']; + $array = explode( '.' , $file['name'] ); + $this -> fileType = strtolower( $array[ count($array) - 1 ] ); + $this -> fileSize = $file['size']; + + $this -> errorNum = $file['error']; + + $this -> newFileName = $this -> getRandowName(); + } + + function getRandowName(){ + $fileName = date('YmdHis') . "_" . rand( 100 , 999 ); + return $fileName . '.' . $this -> fileType; + } + + function savefile(){ + $path = rtrim( 'upload' , '/' ) . '/' ; + $path .= $this -> newFileName; + // var_dump($path); + if (@move_uploaded_file( $this -> tmpFileName , $path )) { + return $path; + }else{ + return false; + } + } +} \ No newline at end of file diff --git a/upload/20160729063426_536.png b/upload/20160729063426_536.png new file mode 100644 index 0000000..1700ca5 Binary files /dev/null and b/upload/20160729063426_536.png differ