类文件:

<?php

class upload{

    protected $fileName;
protected $uploadPath;
protected $maxSize;
protected $extarray;
protected $checkimg;
protected $mimeType;
protected $fileInfo;
protected $error;
protected $ext; public function __construct($fileName='filename',$uploadPath='uploads',$maxSize=2097152,$checkimg=true,$extarray=['jpeg','jpg','png','gif'],$mimeType=['image/jpeg','image/png','image/gif']){
$this->fileName=$fileName;
$this->uploadPath=$uploadPath;
$this->maxSize=$maxSize;
$this->checkimg=$checkimg;
$this->extarray=$extarray;
$this->mimeType=$mimeType;
$this->fileInfo=$_FILES[$this->fileName]; } //判断错误号
protected function checkError(){
if(!is_null($this->fileInfo)){
if($this->fileInfo['error']>0){
switch ($this->fileInfo['error']) {
case 1:
$this->error='文件超过了PHP配置中文件的大小';
break; case 2:
$this->error='文件超过了表单中的限制';
break;
case 3:
$this->error='文件部分被上传';
break;
case 4:
$this->error='没有选择上传文件';
break;
case 6:
$this->error='没有找到临时目录';
break;
case 7:
$this->error='文件不可写';
break;
case 8:
$this->error='由于PHP的扩展程序中断文件上传';
break; }
return false;
}
return true;
}
$this->error='文件上传出错';
return false; } //判断文件大小
protected function checkSize(){
if($this->fileInfo['size']>$this->maxSize){
$this->error='文件过大';
return false;
}
return true; } //判断文件类型
protected function getExt(){
$this->ext=strtolower(pathinfo($this->fileInfo['name'],PATHINFO_EXTENSION));
return $this->ext;
}
protected function checkExt(){
$this->getExt();
if(!in_array($this->ext, $this->extarray)){
$this->error='文件格式不正确,允许的格式为:jpeg,jpg,png,gif';
return false; }
return true;
} //判断mime类型
protected function checkMime(){
if(!in_array($this->fileInfo['type'],$this->mimeType)){
$this->error='不允许的文件类型';
return false;
}
return true;
} //判断是否是真正的图片 protected function checkImage(){
if ($this->checkimg) {
if(@!getimagesize($this->fileInfo['tmp_name'])){
$this->error='不是真正的图片';
return false;
}
return true;
# code...
}
} //判断是否是通过HTTP post方式上传
protected function checkHttppost(){
if(!is_uploaded_file($this->fileInfo['tmp_name'])){
$this->error='不是通过HTTP post方式上传';
return false;
}
return true;
} //显示错误
protected function showError(){
exit('<span style="color:red">'.$this->error.'</span>');
} //检查上传目录并创建
protected function checkPath(){
if(!file_exists($this->uploadPath)){
mkdir($this->uploadPath,0777,true);
}
} //创建唯一的文件名
protected function getName(){
return md5(uniqid(microtime(true),true)).'.'.$this->ext; } //上传文件
public function uploadFile(){
if ($this->checkError()&&$this->checkSize()&&$this->checkExt()&&$this->checkMime()/*&&$this->checkImage()*/&&$this->checkHttppost()) { $this->checkPath();
$this->uniName=$this->getName();
$this->destination=$this->uploadPath.'/'.$this->uniName; if(@move_uploaded_file($this->fileInfo['tmp_name'], $this->destination)){ return $this->destination;
}else{
$this->error='文件移动失败';
$this->showError();
}
# code...
}else{
$this->showError();
}
}
}

表单:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题</title>
</head>
<body>
<form action="doaction.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="2097152">
<input type="file" name="filename" accept="image/jpeg"><br/><br/>
<input type="submit" value="上传">
</form>
</body>
</html>

执行文件:

<?php
require('uploads.class.php'); $upload=new upload('filename','uploads',$maxSize=2097152,$checkimg=false); $dest=$upload->uploadFile(); echo $dest;

PHP上传文件类 代码练习的更多相关文章

  1. 【收集】JAVA多文件 上传文件接口代码 -兼容app

    原文:http://www.verydemo.com/demo_c143_i23854.html 我们在 multifile 中可以很容易的发现如何使用,这里就简单说说了,首先在页面上我们需要有这样几 ...

  2. C# 通用上传文件类

    1.Upfile.aspx: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="U ...

  3. PHP - FTP上传文件类

    /** * 作用:FTP操作类( 拷贝.移动.删除文件/创建目录 ) * 时间:2006/5/9 * 作者:欣然随风 * QQ:276624915 */ class class_ftp { publi ...

  4. PHP上传文件功代码练习(单文件)

    前端: <html> <head><title>upload file</title> <meta http-equiv="Conten ...

  5. asp.net 下载任意格式文件 上传文件后台代码

    思路:将文件转化为流,输出到页面上的iframe中去 //下载附件逻辑 object DownLoad(NameValueCollection nv) { int attachId = nv[&quo ...

  6. 上传文件复用代码【fileUpload】

    这是使用了FileUpload上传组件的,解决了中文乱码问题了,并且删除了临时文件的. 使用了一个Book对象做示范 private Book uploadData(HttpServletReques ...

  7. SpringMVC,SpringBoot上传文件简洁代码

    @RequestMapping("/updateAvatar.html") public String updateHeadUrl(MultipartFile avatar, Mo ...

  8. spring boot 文件上传工具类(bug 已修改)

    以前的文件上传都是之前前辈写的,现在自己来写一个,大家可以看看,有什么问题可以在评论中提出来. 写的这个文件上传是在spring boot 2.0中测试的,测试了,可以正常上传,下面贴代码 第一步:引 ...

  9. iOS上传文件代码,自定义组装body

    以下代码为上传文件所用代码,简单方便,搞了好久,终于知道这么简单的方式来上传. 其它类库也就是把这几句代码封装的乱七八糟得,让你老久搞不懂原理.不就是在body上面加点字符串,body下面加点字符串, ...

随机推荐

  1. python多线程编程(6): 队列同步

    原文请看:http://www.cnblogs.com/holbrook/archive/2012/03/15/2398060.html 前面介绍了互斥锁和条件变量解决线程间的同步问题,并使用条件变量 ...

  2. lamp字符编码的转换规则

    1.lamp字符编码的转换规则 lamp(Linux+Apache+Mysql+PHP) 1.1GB 2312 GB 2312 或 GB 2312-80 是中国国家标准简体中文字符集,全称<信息 ...

  3. 关于在eclipse下的mapreduce工程打包成jar包的问题(包含第三方jar包)

    这个问题也是在开发项目中经常遇到的一个问题,网上提供了很多方法,但是我发现很多并不适用,这里推荐两种方法,一种肯定没问题,就是比较麻烦,另一种是适用FatJar来打包,但是我没成功,原因估计出在ubu ...

  4. 167. Two Sum II - Input array is sorted【Easy】【双指针-有序数组求两数之和为目标值的下标】

    Given an array of integers that is already sorted in ascending order, find two numbers such that the ...

  5. Spring Cloud Feign 总结

    Spring Cloud中, 服务又该如何调用 ? 各个服务以HTTP接口形式暴露 , 各个服务底层以HTTP Client的方式进行互相访问. SpringCloud开发中,Feign是最方便,最为 ...

  6. curator管理es索引

    安装curator------------------rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch vi /etc/yu ...

  7. 【Java NIO】一文了解NIO

    Java NIO 1 背景介绍 在上一篇文章中我们介绍了Java基本IO,也就是阻塞式IO(BIO),在JDK1.4版本后推出了新的IO系统(NIO),也可以理解为非阻塞IO(Non-Blocking ...

  8. 文件包含漏洞检测工具fimap

    文件包含漏洞检测工具fimap   在Web应用中,文件包含漏洞(FI)是常见的漏洞.根据包含的文件不同,它分为本地文件包含漏洞(LFI)和远程文件包含漏洞(RFL).利用该漏洞,安全人员可以获取服务 ...

  9. Android,几款apk工具

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 可以查看到链接的设备 可以查看到wifi密码,这个也是个开源的 可以查询驾照学时进度 可 ...

  10. WSDL格式浅析

    其中,WSDL是一种 XML 格式,用于将网络服务描述为一组端点,这些端点对包含面向文档信息或面向过程信息的消息进行操作.这种格式首先对操作和消息进行抽象描述,然后将其绑定到具体的网络协议和消息格式上 ...