需求分析如下:

(1)支持单个文件上传
(2)支持多个文件上传
(3)可以指定保存位置 可以设置上传文件允许的大小和类型 可以选择重命名和保留原名

 <!--
设计一个经典文件上传类
需求分析
(1)支持单个文件上传
(2)支持多个文件上传
(3)可以指定保存位置 可以设置上传文件允许的大小和类型 可以选择重命名和保留原名
程序设计
成员属性
成员方法 --> <?php
error_reporting(E_ALL && !E_NOTICE); class FileUpload{
private $path = "./uploads";
private $allowtype = array('jpg','gif','png','txt');
private $maxsize = 1000000;
private $israndname = true;//设置是否随机重命名文件 private $originName;
private $tmpFileName;
private $fileType;
private $fileSize;
private $newFileName;
private $errorNum = 0;
private $errorMess = ""; /**
* 用于设置成员属性
* 可以通过连贯操作设置多个值
*/
function set($key,$val){
$key = strtolower($key);
if(array_key_exists($key,get_class_vars(get_class($this)))){
$this -> setOption($key,$val);
}
return $this;
} /**
* 用该函数来上传文件
*/
function upload($fileField){
$return = true; //检查文件路径是否合法
if(!$this -> checkFilepath()){
$this -> errorMess = $this -> errorMess();
return false;
}
//将文件上传的信息取出赋值给变量
$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()){
$error[] = $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();
//上传文件 成功返回true 失败返回false
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;
}
} /**
* 获取上传后的文件名称
*/
public function getFileName(){
return $this -> newFileName;
} /**
* 上传失败后,调用该方法则调用该方法返回,上传出错信息
*/
public function getErrorMsg(){
return $this -> errorMess;
} //设置上传错误信息
private function getError(){
$str = "上传文件<font color = 'red'>{$this -> originName}</font>时出错 :";
switch($this -> errorNum){
case 4:$str .= "没有文件被上传";break;
case 3:$str .= "文件只有部分被上传";break;
case 2:$str .= "上传文件的大小超过了HTML表单中指定的值";break;
case 1:$str .= "上传文件的大小超过了php.ini中指定的值";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.'<br>';
} //设置和$_FILES有关的内容
private function setFiles($name = "",$tmp_name = "",$size = "",$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)){
$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;
if(@move_uploaded_file($this -> tmpFileName,$path)){
return true;
}else{
$this -> setOption('errorNum',-3);
return false;
}
}else{
return false;
}
} }
?>

上面为一个文件上传类 想要用到这个类需要另写一个PHP文件来调用该类

内容如下

 <meta http-equiv="Content-Type" content="text/html;charset=utf-8">

 <?php
/**
使用文件上传类fileupload.class.php 处理单个或多个文件上传
*/ error_reporting(E_ALL && !E_NOTICE);
require "fileupload.class.php";//将文件上传类的文件包含进来 $up = new FileUpload; //可以通过set方法设置上传的属性 设置多个属性set方法可以单独调用 也可以连贯操作一起调用多个
/* $up -> set('path','./newpath/')
-> set('size',1000000)
-> set('allowtype',array('gif','png','jpg','txt'))
-> set('israndname',false);
*/
//调用$up对象的upload方法上传文件 myfile是表单的名称 上传成功返回true 否则返回false
if($up -> upload('myFile')){
//如果是多个文件 返回数组 存放所有上传后的文件名 单文件上传则直接返回文件名称
print_r($up -> getFileName());
}else{
//如果是多个文件 返回数组 多条出错信息 单文件上传则直接返回一条错误报告
print_r($up -> getErrorMsg());
}
?>

运用问价上传类

然后在写一个HTML文件作为前端 使用上面运用到文件上传类的文件

单文件上传:

 <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<html>
<head><title>单个文件上传</title></head> <body>
<form action = "upload.php" method = "POST" enctype = "multipart/form-data">
<input type = "hidden" name = "MAXN_FILIE_SIZE" value = "100000">
选择文件:<input type = "file" name = "myFile">
<input type = "submit" value = "上传文件">
</form>
</body>
</html>

singleFileUpload

多文件上传:

 <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<html>
<head><title>单个文件上传</title></head> <body>
<form action = "upload.php" method = "POST" enctype = "multipart/form-data">
<input type = "hidden" name = "MAXN_FILIE_SIZE" value = "100000">
选择文件1:<input type = "file" name = "myFile[]">
选择文件2:<input type = "file" name = "myFile[]">
选择文件3:<input type = "file" name = "myFile[]">
<input type = "submit" value = "上传文件">
</form> </body>
</html>

multiFileUpload

一个经典的PHP文件上传类的更多相关文章

  1. php 文件上传类,功能相当齐全,留作开发中备用吧。

    收藏一个经典好用的php 文件上传类,功能相当齐全,留作开发中备用吧. 好东西,大家都喜欢,才是真的好,哈哈!!! <?php  /**   * 文件上传类   */  class upload ...

  2. PHP 文件上传类

    FileUpload.;                $];                $_newname = date(,). :                             To ...

  3. php 文件上传类 实例分享

    最近在研究php上传的内容,找到一个不错的php上传类,分享下. <?php /** * 文件上传类 * class: uploadFile * edit: www.jbxue.com */ c ...

  4. Php文件上传类class.upload.php

    简介 Class.upload.php是用于管理上传文件的php文件上传类, 它可以帮助你快速的给自己的网站集成上传文件功能.不仅如此,此分类还有一些列的处理功能,可以对上传的文件或者本地的文件进行处 ...

  5. PHP文件上传类(页面和调用部分)

    <!--upform.html内容--> <form action="upload.php" method="post" enctype=&q ...

  6. php多文件上传类(含示例)

    在网上看到一个比较好的多文件上传类,自己改良了下,顺便用js实现了多文件浏览,php文件上传原理都是相同的,多文件上传也只是进行了循环上传而已,当然你也可以使用swfupload进行多文件上传! &l ...

  7. ASP.NET 文件上传类 简单好用

    调用: UploadFile uf = new UploadFile(); /*可选参数*/ uf.SetIsUseOldFileName(true);//是否使用原始文件名作为新文件的文件名(默认: ...

  8. [上传下载] C#FileUp文件上传类 (转载)

    点击下载 FileUp.zip 主要功能如下 .把上传的文件转换为字节数组 .流转化为字节数组 .上传文件根据FileUpload控件上传 .把Byte流上传到指定目录并保存为文件 看下面代码吧 // ...

  9. ThinkPHP文件上传类

    TP框架自带文件上传类使用: 类文件在ThinkPHP/Library/Think/默认在目录下 public function upload(){ $upload = new \Think\Uplo ...

随机推荐

  1. HTML a标签如何设置margin属性(转)

    很多同学发现对DIV有效的许多CSS属性对<a>或<p>标签都无效,好比说 <div style="margin-top:5px;"></ ...

  2. BZOJ5443:[CEOI2018]Lottery

    我对状态空间的理解:https://www.cnblogs.com/AKMer/p/9622590.html 题目传送门:https://www.lydsy.com/JudgeOnline/probl ...

  3. art-template-loader:template

    ylbtech-art-template-loader: 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:ht ...

  4. 【转载】ruby 中数组函数示例(1)(转)

    函数名称 说明 示例 &      数组与,返回两数组的交集 [1,2] & [2,3] =>[2]   * 复制数组n次 [1,2]*2       => [1,2,1, ...

  5. Java学习路线-知乎

    鼬自来晓 378 人赞同 可以从几方面来看Java:JVM Java JVM:内存结构和相关参数含义 · Issue #24 · pzxwhc/MineKnowContainer · GitHub J ...

  6. 10、RNA-seq for DE analysis training(Mapping to assign reads to genes)

    1.Goal of mapping 1)We want to assign reads to genes they were derived from 2)The result of the mapp ...

  7. 嵌入式linux环境搭建

    花了两天时间,终于搭建好了板子上的linux驱动开发环境,不容易呀,做个笔记. 首先搭建PC上的编译环境,因为编译的驱动是在板子上运行的,第一步当然需要安装交叉编译器,即arm-none-linux- ...

  8. iscsi使用教程(中)

    服务端管理命令 ### tgtadm 是一个模式化的命令,其使用格式如下: # tgtadm --lld [driver] --op [operation] --mode [mode] [OPTION ...

  9. 2010辽宁省赛F(字典树,动态规划)

    #include<bits/stdc++.h>using namespace std;int n,x;char s[10010];char a[31010];int val[100010] ...

  10. C - Trailing Zeroes (III)(二分)

    You task is to find minimal natural number N, so that N! contains exactly Q zeroes on the trail in d ...