类文件:

<?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. Vue 2.0 Application Sample

    ===搭建Demo=== http://blog.csdn.net/wangjiaohome/article/details/51728217 ===单页Application=== http://b ...

  2. Visual Studio找不到adb.exe错误解决

    Visual Studio找不到adb.exe错误解决 错误信息:Cannot find adb.exe in specified SDK path.出现这种情况,是因为没有安装Android SDK ...

  3. Spring和依赖注入的价值

    javaeye上看到有帖子,置疑spring和依赖注入的价值,回复内容整理如下: 依赖注入对设计有利,而spring则促进了依赖注入的使用. 如果业务处理类,它所使用的倚赖,都是依靠在这个类内部实现或 ...

  4. 【转载】R中有关数据挖掘的包

    下面列出了可用于数据挖掘的R包和函数的集合.其中一些不是专门为了数据挖掘而开发,但数据挖掘过程中这些包能帮我们不少忙,所以也包含进来. 1.聚类 常用的包: fpc,cluster,pvclust,m ...

  5. [BZOJ1444]有趣的游戏(AC自动机+矩阵乘法)

    n个等长字符串,机器会随机输出一个字符串(每个字母出现的概率为p[i]),问每个字符串第一个出现的概率是多少. 显然建出AC自动机,套路地f[i][j]表示i时刻位于节点j的概率. 构建转移矩阵,当i ...

  6. JZYZOJ1349 SPOJ839 星屑幻想 xor 网络流 最大流

    http://172.20.6.3/Problem_Show.asp?id=1349 调了两个小时发现数组开小了[doge].题意:给出几个点,有的点的权值确定,连接两点的边的权值为两点值的异或和,求 ...

  7. 「2018山东一轮集训」Game

    %%神仙题 首先转化一波模型:可以把原问题看成,初始每个位置有0/1个石子,1操作看成从一个位置拿走一个石子,2操作看成从l[i]拿走一个石子,并在[ l[i]+1 , r[i] ]的每个位置放上一个 ...

  8. HZAU 1201 Friends(树形DP)

    [题目链接] http://acm.hzau.edu.cn/problem.php?id=1201 [题目大意] 给出一棵树,问每个节点距离六个点以内的点有几个 [题解] 定根维护树形DP,Dw[x] ...

  9. 【最小割】【Dinic】Gym - 101128F - Landscaping

    http://blog.csdn.net/lxy767087094/article/details/68942422 #include<cstdio> #include<cstrin ...

  10. Node.js+MySQL管理工作的详细信息所遇到的问题

    问题陈述: Error: ER_TRUNCATED_WRONG_VALUE_FOR_FIELD: Incorrect string value: '\xE8\xB4\xAD\xE7\x89\xA9' ...