2020年5月29日
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>内容</title>
</head
<body>
h1>单文件上传</h1>
<form action="demo.php" method="post" enctype="multipart/form-data">
<input type="file" name="f">、
<button>上传</button>
</form>
<h1>多文件上传</h1>
<form action="demo.php" method="post" enctype="multipart/form-data">
<input type="file" name="f[]">
<input type="file" name="f[]">
<input type="file" name="f[]">
<button>上传</button>
</form>
</body>
</html>
<?php
include './Upload.class.php';
$obj=new Upload('f');
var_dump($obj);
class Upload{
protected $fileInfo;
protected $errorMsg;
protected $size =1024*1024*10;
protected $type =[
'image/png',
'image/jpg',
'image/gif'
];
protected $rootPath='./uploads/';
public function __construct($name){
$this->fileInfo=$_FILES[$name];
}
public function geterrorMsg(){
return $this->errorMsg;
}
public function $size($data=null){
if ($data !== null && is_numeric($data)){
$this->size = $data;
}
return $this->size;
}
public function type($data=null){
if($data !== null && is_array($data)){
$this->type =$data;
}
return $this->type;
}
public function rootPath($data=null){
if ($data !== null && file_exists($data)){
$data = rtrim($data,'/').'/';
$this->rootPath = $data;
}
return $this->rootPath;
}
}
include './Upload.class.php';
$obj=new Upload('f');
$obj->size(1024*1024*10);
if($obj->upload()){
}else{
echo $obj->geterrorMsg();
}
class Upload{
protected $fileInfo;
protected $errorMsg;
protected $size =1024*1024*10;
protected $type =[
'image/png',
'image/jpg',
'image/gif',
'application/x-zip-dompressed',
'application/vnd.ms-excel',
];
protected $rootPath='./uploads/';
public function __construct($name){
$this->fileInfo=$_FILES[$name];
}
public function upload(){
$error = $this->fileInfo['error'];
$size=$this->fileInfo['size'];
$type=$this->fileInfo['type'];
$checkRes=$this->check($error,$size,$type);
if($checkRes == false);
}
}
public function geterrorMsg(){
return $this->errorMsg;
}
public function $size($data=null){
if ($data !== null && is_numeric($data)){
$this->size = $data;
}
return $this->size;
}
public function type($data=null){
if($data !== null && is_array($data)){
$this->type =$data;
}
return $this->type;
}
public function rootPath($data=null){
if ($data !== null && file_exists($data)){
$data = rtrim($data,'/').'/';
$this->rootPath = $data;
}
return $this->rootPath;
}
protected function check($error,$size,$type){
if($this->checkError($error)==false){
return false;
}
if($this->checkSize($size)==false){
return false;
}
return true;
}
protected function checkError($error)
{
if($error >0){
switch($error){
case 1:
$this->errorMsg('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值');
break;
case 2:
$this->errorMsg('上传的文件大小超过HTML表单中MAX_FILE_SIZE 选项指定的值');
break;
case 3:
$this->errorMsg('文件只有部分上传');
break;
case 4:
$this->errorMsg('没有文件被上传');
break;
case 6:
$this->errorMsg('找不到临时文件夹');
break;
case 7:
$this->errorMsg('文件写入失败');
break;
}
return false;
}
return true;
}
protected function checkSize($size){
if($size>$this->size){
$this->errorMsg="超出大小";
return false;
}
return true;
}
protected function checkType($type){
if(! in_array($type,$this->type)){
$this->errorMsg='不被允许上传的类型:'.$type;
return false;
}
return true;
}
protected function generateName($tmp_name,$name){
$fileData=file_get_contents(tmp_name);
$fileMd5=md5($fileData);
$id=uniqid();
//获得文件后缀
$fileExt=pathinfo($file['name'],PATHINFO_EXTENSION);
//拼接成文件名
$fileName=$fileMd5 .'-'.$id.'.'.$fileExt;
return $fileName;
}
protected function sava($tmp_name,$fileName){
$path=$this->rootPath .$filename;
if(is_uploaded_file($tmp_name)&& move_uploaded_file($tmp_name,$path)){
return $path;
}else{
$this->errorMsg ="上传失败";
return false;
}
}
}
include './Upload.class.php';
$obj=new Upload('f');
$obj->size(1024*1024*10);
$obj->type([
'image/png',
'application/x-masdownload',
]);
if($obj->upload()){
echo "上传成功";
var_dump($obj->getFilePath());
}else{
echo $obj->getErrorMsg();
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>内容</title>
</head
<body>
h1>单文件上传</h1>
<form action="demo.php" method="post" enctype="multipart/form-data">
<input type="file" name="f">、
<button>上传</button>
</form>
<h1>多文件上传</h1>
<form action="demo.php" method="post" enctype="multipart/form-data">
<input type="file" name="f[]">
<input type="file" name="f[]">
<input type="file" name="f[]">
<button>上传</button>
</form>
</body>
</html>
<?php
include './Upload.class.php';
$obj=new Upload('f');
var_dump($obj);
class Upload{
protected $fileInfo;
protected $errorMsg;
protected $size =1024*1024*10;
protected $type =[
'image/png',
'image/jpg',
'image/gif'
];
protected $rootPath='./uploads/';
public function __construct($name){
$this->fileInfo=$_FILES[$name];
}
public function geterrorMsg(){
return $this->errorMsg;
}
public function $size($data=null){
if ($data !== null && is_numeric($data)){
$this->size = $data;
}
return $this->size;
}
public function type($data=null){
if($data !== null && is_array($data)){
$this->type =$data;
}
return $this->type;
}
public function rootPath($data=null){
if ($data !== null && file_exists($data)){
$data = rtrim($data,'/').'/';
$this->rootPath = $data;
}
return $this->rootPath;
}
}
include './Upload.class.php';
$obj=new Upload('f');
$obj->size(1024*1024*10);
if($obj->upload()){
}else{
echo $obj->geterrorMsg();
}
class Upload{
protected $fileInfo;
protected $errorMsg;
protected $size =1024*1024*10;
protected $type =[
'image/png',
'image/jpg',
'image/gif',
'application/x-zip-dompressed',
'application/vnd.ms-excel',
];
protected $rootPath='./uploads/';
public function __construct($name){
$this->fileInfo=$_FILES[$name];
}
public function upload(){
$error = $this->fileInfo['error'];
$size=$this->fileInfo['size'];
$type=$this->fileInfo['type'];
$checkRes=$this->check($error,$size,$type);
if($checkRes == false);
}
}
public function geterrorMsg(){
return $this->errorMsg;
}
public function $size($data=null){
if ($data !== null && is_numeric($data)){
$this->size = $data;
}
return $this->size;
}
public function type($data=null){
if($data !== null && is_array($data)){
$this->type =$data;
}
return $this->type;
}
public function rootPath($data=null){
if ($data !== null && file_exists($data)){
$data = rtrim($data,'/').'/';
$this->rootPath = $data;
}
return $this->rootPath;
}
protected function check($error,$size,$type){
if($this->checkError($error)==false){
return false;
}
if($this->checkSize($size)==false){
return false;
}
return true;
}
protected function checkError($error)
{
if($error >0){
switch($error){
case 1:
$this->errorMsg('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值');
break;
case 2:
$this->errorMsg('上传的文件大小超过HTML表单中MAX_FILE_SIZE 选项指定的值');
break;
case 3:
$this->errorMsg('文件只有部分上传');
break;
case 4:
$this->errorMsg('没有文件被上传');
break;
case 6:
$this->errorMsg('找不到临时文件夹');
break;
case 7:
$this->errorMsg('文件写入失败');
break;
}
return false;
}
return true;
}
protected function checkSize($size){
if($size>$this->size){
$this->errorMsg="超出大小";
return false;
}
return true;
}
protected function checkType($type){
if(! in_array($type,$this->type)){
$this->errorMsg='不被允许上传的类型:'.$type;
return false;
}
return true;
}
protected function generateName($tmp_name,$name){
$fileData=file_get_contents(tmp_name);
$fileMd5=md5($fileData);
$id=uniqid();
//获得文件后缀
$fileExt=pathinfo($file['name'],PATHINFO_EXTENSION);
//拼接成文件名
$fileName=$fileMd5 .'-'.$id.'.'.$fileExt;
return $fileName;
}
protected function sava($tmp_name,$fileName){
$path=$this->rootPath .$filename;
if(is_uploaded_file($tmp_name)&& move_uploaded_file($tmp_name,$path)){
return $path;
}else{
$this->errorMsg ="上传失败";
return false;
}
}
}
include './Upload.class.php';
$obj=new Upload('f');
$obj->size(1024*1024*10);
$obj->type([
'image/png',
'application/x-masdownload',
]);
if($obj->upload()){
echo "上传成功";
var_dump($obj->getFilePath());
}else{
echo $obj->getErrorMsg();
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>内容</title>
</head
<body>
h1>单文件上传</h1>
<form action="demo.php" method="post" enctype="multipart/form-data">
<input type="file" name="f">、
<button>上传</button>
</form>
<h1>多文件上传</h1>
<form action="demo.php" method="post" enctype="multipart/form-data">
<input type="file" name="f[]">
<input type="file" name="f[]">
<input type="file" name="f[]">
<button>上传</button>
</form>
</body>
</html>
<?php
include './Upload.class.php';
$obj=new Upload('f');
var_dump($obj);
class Upload{
protected $fileInfo;
protected $errorMsg;
protected $size =1024*1024*10;
protected $type =[
'image/png',
'image/jpg',
'image/gif'
];
protected $rootPath='./uploads/';
public function __construct($name){
$this->fileInfo=$_FILES[$name];
}
public function geterrorMsg(){
return $this->errorMsg;
}
public function $size($data=null){
if ($data !== null && is_numeric($data)){
$this->size = $data;
}
return $this->size;
}
public function type($data=null){
if($data !== null && is_array($data)){
$this->type =$data;
}
return $this->type;
}
public function rootPath($data=null){
if ($data !== null && file_exists($data)){
$data = rtrim($data,'/').'/';
$this->rootPath = $data;
}
return $this->rootPath;
}
}
include './Upload.class.php';
$obj=new Upload('f');
$obj->size(1024*1024*10);
if($obj->upload()){
}else{
echo $obj->geterrorMsg();
}
class Upload{
protected $fileInfo;
protected $errorMsg;
protected $size =1024*1024*10;
protected $type =[
'image/png',
'image/jpg',
'image/gif',
'application/x-zip-dompressed',
'application/vnd.ms-excel',
];
protected $rootPath='./uploads/';
public function __construct($name){
$this->fileInfo=$_FILES[$name];
}
public function upload(){
$error = $this->fileInfo['error'];
$size=$this->fileInfo['size'];
$type=$this->fileInfo['type'];
$checkRes=$this->check($error,$size,$type);
if($checkRes == false);
}
}
public function geterrorMsg(){
return $this->errorMsg;
}
public function $size($data=null){
if ($data !== null && is_numeric($data)){
$this->size = $data;
}
return $this->size;
}
public function type($data=null){
if($data !== null && is_array($data)){
$this->type =$data;
}
return $this->type;
}
public function rootPath($data=null){
if ($data !== null && file_exists($data)){
$data = rtrim($data,'/').'/';
$this->rootPath = $data;
}
return $this->rootPath;
}
protected function check($error,$size,$type){
if($this->checkError($error)==false){
return false;
}
if($this->checkSize($size)==false){
return false;
}
return true;
}
protected function checkError($error)
{
if($error >0){
switch($error){
case 1:
$this->errorMsg('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值');
break;
case 2:
$this->errorMsg('上传的文件大小超过HTML表单中MAX_FILE_SIZE 选项指定的值');
break;
case 3:
$this->errorMsg('文件只有部分上传');
break;
case 4:
$this->errorMsg('没有文件被上传');
break;
case 6:
$this->errorMsg('找不到临时文件夹');
break;
case 7:
$this->errorMsg('文件写入失败');
break;
}
return false;
}
return true;
}
protected function checkSize($size){
if($size>$this->size){
$this->errorMsg="超出大小";
return false;
}
return true;
}
protected function checkType($type){
if(! in_array($type,$this->type)){
$this->errorMsg='不被允许上传的类型:'.$type;
return false;
}
return true;
}
protected function generateName($tmp_name,$name){
$fileData=file_get_contents(tmp_name);
$fileMd5=md5($fileData);
$id=uniqid();
//获得文件后缀
$fileExt=pathinfo($file['name'],PATHINFO_EXTENSION);
//拼接成文件名
$fileName=$fileMd5 .'-'.$id.'.'.$fileExt;
return $fileName;
}
protected function sava($tmp_name,$fileName){
$path=$this->rootPath .$filename;
if(is_uploaded_file($tmp_name)&& move_uploaded_file($tmp_name,$path)){
return $path;
}else{
$this->errorMsg ="上传失败";
return false;
}
}
}
include './Upload.class.php';
$obj=new Upload('f');
$obj->size(1024*1024*10);
$obj->type([
'image/png',
'application/x-masdownload',
]);
if($obj->upload()){
echo "上传成功";
var_dump($obj->getFilePath());
}else{
echo $obj->getErrorMsg();
}
笔记:
封装一个类:文件上传的功能
面向过程:按照操作(功能:文件上传)来编写,一步一步按照顺序去实现功能
面向对象:按照功能(将文件上传功能下的子功能拆分为一个一个的方法),将子功能抽象成为方法,然后把描述抽象成为属性
对象:文件上传功能
属性:文件信息 错误信息 文件最大上传字节 文件限定的类型 文件根保存目录
方法:接受文件信息 判断是否有错误 判断大小是否超出 判断文件类型是否允许上传 保存文件的操作