类文件:

<?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. PHP7 微信支付回调失败 解决

    升级完PHP7 发现微信支付回调失败.原来是 $GLOBALS['HTTP_RAW_POST_DATA'];没有定义的问题.php7 移除了这个全局变量. 问题代码如下: 微信API :WxPay.A ...

  2. file '/grub/i386-pc/normal.mod' not found.解决方案

    前言: 因为之前装的Ubuntu出了点问题,本想直接清除Ubuntu数据重新装一下,结果蹦出这么个BUG来,揪心,弄了大半天终于弄好了. 废话不多说,直接按教程走吧. GRUB启动: 在grub启动界 ...

  3. 缓存(LruCache)机制

    LruCache 1.变量 private final LinkedHashMap<K, V> map; private int size;//已经存储的数据大小 private int ...

  4. ZOJ 3211 Dream City

    贪心,$dp$. 假设我们知道要选择哪些物品,那么这些物品应该按什么顺序选择呢? 物品$A(a1,b1)$,物品$B(a2,b3)$. 假设物品$A$在第$x$天被选择,物品$B$在第$y$天被选择. ...

  5. 循序渐进PYTHON3(十三) --2-- DJANGO之FORM表单(自动生成HTML标签和自定制提示信息)

    在上一次的代码上做出进一步修改,使之能在页面上显示自定制的报错信息,并且使用form自动创建标签的功能. views.py from django.shortcuts import render,Ht ...

  6. RabbitMQ (十) 远程过程调用(RPC)

    在远程计算机上运行一个函数并等待结果,我们通常叫这种模式为远程过程调用或者RPC. 通过 RabbitMQ 进行 RPC 很容易,客户端发送请求消息,服务器回复响应消息.为了接收响应,我们需要发送带有 ...

  7. noip 2008 传纸条

    题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法直接交谈了.幸运的是 ...

  8. poj 3225 Help with Intervals(线段树,区间更新)

    Help with Intervals Time Limit: 6000MS   Memory Limit: 131072K Total Submissions: 12474   Accepted:  ...

  9. 【bitset】【推导】hdu5961 传递

    <法一>http://blog.csdn.net/u014325920/article/details/53046890 1.判断传递的条件为:若G中有 一条边从a到b且有一条边从b到c ...

  10. 【动态规划技巧题】POJ2229-Sumsets

    [题目大意] 把一个数n分成2的指数幂相加的形式,问有几种情况. [思路] 如果当前i为奇数,则必定有至少一个1,可以看作i-1的情形再加上一个1.即f[i]=f[i-1]. 如果当前i为偶数,假设没 ...