类文件:

<?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. TCP连接复用

    转自网络:看到一陌生名词,记录一下 TCP连接复用技术通过将前端多个客户的HTTP请求复用到后端与服务器建立的一个TCP连接上.这种技术能够大大减小服务器的性能负载,减少与服务器之间新建TCP连接所带 ...

  2. 一个纠结的Linux定时任务

    昨天写了一个Linux定时任务,搞了半天才是搞好,现在分享下我犯得错误 首先在Linux根目录下创建一个目录 mkdir cat_crazy 进去创建一个shell脚本test.sh,内容是: #!/ ...

  3. 【java NIO】服务器端读写图片的一次排错经历

    上传文件方面: 一.前端 使用的是jQuery框架来上传图片,参考的是harttle大神博客:http://harttle.com/2016/07/04/jquery-file-upload.html ...

  4. 最近升级mysql5.7出现下面问题,ORDER BY clause is not in GROUP BY..this is incompatible with sql_mode=only_full_group_by

    执行sql: SELECT * FROM `user_link` WHERE `group_id` IN ('78', '79') GROUP BY `link_id` 报错: SQLSTATE[42 ...

  5. [bzoj3244][noi2013]树的计数 题解

    UPD: 那位神牛的题解更新了,在这里. ------------------------------------------------------------------------------- ...

  6. 【分块】【bitset】hdu6085 Rikka with Candies

    给你数组A和B,A B中的元素大小都不超过5w,且两两不同. q次询问,每次给你个k,问你有多少对(i,j),满足A(i)%B(j)==k. 如题目所言模拟bitset的过程,实质上是个分块,每块的大 ...

  7. 重拾vue2

    Vue组件 一.组件介绍 每一个组件都是一个vue实例 每个组件均具有自身的模板template,根组件的模板就是挂载点 每个组件模板只能拥有一个根标签 子组件的数据具有作用域,以达到组件的复用 二. ...

  8. codevs 3641 上帝选人

    3641 上帝选人  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题目描述 Description 世界上的人都有智商IQ和情商EQ.我们用两个数字来表示人的 ...

  9. [转]SpringMVC拦截器详解[附带源码分析]

      目录 前言 重要接口及类介绍 源码分析 拦截器的配置 编写自定义的拦截器 总结 前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:ht ...

  10. Codeforces Round #127 (Div. 1) E. Thoroughly Bureaucratic Organization 二分 数学

    E. Thoroughly Bureaucratic Organization 题目连接: http://www.codeforces.com/contest/201/problem/E Descri ...