这里演示了FileUpload.class.php文件上传类,其中用到了两个常量,可在网站配置文件中定义:
1
2
|
define('ROOT_PATH',dirname(__FILE__)); //网站根目录
define('UPDIR','/uploads/'); //上传主目录
|
具体代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
<?php
//上传文件类
class FileUpload {
private $error; //错误代码
private $maxsize; //表单最大值
private $type; //类型
private $typeArr = array('image/jpeg','image/pjpeg','image/png','image/x-png','image/gif'); //类型合集
private $path; //目录路径
private $today; //今天目录
private $name; //文件名
private $tmp; //临时文件
private $linkpath; //链接路径
private $linktotay; //今天目录(相对)
//构造方法,初始化
public function __construct($_file,$_maxsize) {
$this->error = $_FILES[$_file]['error'];
$this->maxsize = $_maxsize / 1024;
$this->type = $_FILES[$_file]['type'];
$this->path = ROOT_PATH.UPDIR;
$this->linktotay = date('Ymd').'/';
$this->today = $this->path.$this->linktotay;
$this->name = $_FILES[$_file]['name'];
$this->tmp = $_FILES[$_file]['tmp_name'];
$this->checkError();
$this->checkType();
$this->checkPath();
$this->moveUpload();
}
//返回路径
public function getPath() {
$_path = $_SERVER["SCRIPT_NAME"];
$_dir = dirname(dirname($_path));
if ($_dir == '\') $_dir = '/';
$this->linkpath = $_dir.$this->linkpath;
return $this->linkpath;
}
//移动文件
private function moveUpload() {
if (is_uploaded_file($this->tmp)) {
if (!move_uploaded_file($this->tmp,$this->setNewName())) {
Tool::alertBack('警告:上传失败!');
}
} else {
Tool::alertBack('警告:临时文件不存在!');
}
}
//设置新文件名
private function setNewName() {
$_nameArr = explode('.',$this->name);
$_postfix = $_nameArr[count($_nameArr)-1];
$_newname = date('YmdHis').mt_rand(100,1000).'.'.$_postfix;
$this->linkpath = UPDIR.$this->linktotay.$_newname;
return $this->today.$_newname;
}
//验证目录
private function checkPath() {
if (!is_dir($this->path) || !is_writeable($this->path)) {
if (!mkdir($this->path)) {
Tool::alertBack('警告:主目录创建失败!');
}
}
if (!is_dir($this->today) || !is_writeable($this->today)) {
if (!mkdir($this->today)) {
Tool::alertBack('警告:子目录创建失败!');
}
}
}
//验证类型
private function checkType() {
if (!in_array($this->type,$this->typeArr)) {
Tool::alertBack('警告:不合法的上传类型!');
}
}
//验证错误
private function checkError() {
if (!empty($this->error)) {
switch ($this->error) {
case 1 :
Tool::alertBack('警告:上传值超过了约定最大值!');
break;
case 2 :
Tool::alertBack('警告:上传值超过了'.$this->maxsize.'KB!');
break;
case 3 :
Tool::alertBack('警告:只有部分文件被上传!');
break;
case 4 :
Tool::alertBack('警告:没有任何文件被上传!');
break;
default:
Tool::alertBack('警告:未知错误!');
}
}
}
}
?>
|
其中,用到了一个静态工具类 Tool.class.php,代码如下:
Tool.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<?php
class Tool {
//弹窗返回
static public function alertBack($_info) {
echo "<script type='text/javascript'>alert('$_info');history.back();</script>";
exit();
} //弹窗赋值关闭
static public function alertOpenerClose($_info,$_path) {
echo "<script type='text/javascript'>alert('$_info');</script>";
echo "<script type='text/javascript'>opener.document.content.thumbnail.value='$_path';</script>";
echo "<script type='text/javascript'>opener.document.content.pic.style.display='block';</script>";
echo "<script type='text/javascript'>opener.document.content.pic.src='$_path';</script>";
echo "<script type='text/javascript'>window.close();</script>";
exit();
} }
?>
|
下面进行一个实例演示,请看下面的步骤:
1、先创建一个 index.php 页面,做一个表单
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>main</title>
</head>
<body>
<form name="content" method="post" action="?action=add">
<input type="text" name="thumbnail" class="text" readonly="readonly" /> <input type="button" value="上传" onclick="centerWindow('./upfile.html','upfile','400','100')" /> <img name="pic" style="display:none;" /> ( * 必须是jpg,gif,png,并且200k内) <br />
</form>
</body>
</html>
|
2、创建 upfile.html 文件,建立表单提交到 upload.php
upfile.html
1
2
3
4
5
6
7
8
9
10
11
12
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>上传图片</title>
</head>
<body></p><p> <form method="post" action="./upload.php" enctype="multipart/form-data" style="text-align:center;margin:30px;">
<input type="hidden" name="MAX_FILE_SIZE" value="204800" />
<input type="file" name="pic" />
<input type="submit" name="send" value="确定上传" />
</form></p><p></body>
</html>
|
3、通过 upload.php 文件调用文件上传类实现上传,并且把路径赋给 input 标签和显示图片
1
2
3
4
5
6
7
8
9
10
|
<?php
require 'FileUpload.class.php';
if (isset($_POST['send'])) {
$_fileupload = new FileUpload('pic',$_POST['MAX_FILE_SIZE']);
$_path = $_fileupload->getPath();
Tool::alertOpenerClose('文件上传成功!',$_path);
} else {
Tool::alertBack('警告:文件过大或者其他未知错误导致浏览器崩溃!');
}
?>
|
京喜app是一款京东旗下推出的网购电商平台,京喜app里面能够帮助你省下的大量的金额,很多的优惠卷免费发送,先领劵在购物省一大笔钱。还有每天推荐精品好货为你提供最优质的商品,精细分类让你挑选起来更简易,需要的用户快来下载体验把。
旅行串串游戏下载是一款非常好玩的休闲经营养成类型游戏,旅行串串游戏安卓下载在游戏剧情里,你跟女友辞去了工作,来了一场说走就走的旅行,旅途中你会看到世界各地的文化、风景、美食等。非常的有意思。赶快下载试试。
泡沫冬景破解版是一款今年超级火爆galgame。泡沫冬景破解免费版游戏正版是要收费18元才能玩的,小编在这里找到了一个免费的下载渠道,但内容是跟正版一模一样的哦。喜欢这款游戏的欢迎来ucbug下载站下载体验。
百度地图2020最新版是一款最新的地图导航手机软件,百度地图2020最新手机版软件内有着全网最新的地图路线,而且增添了许多实用功能,可以看到公交车的实时定位,也可以直接在地图软件上预估打车价格等等。非常方便。
高德地图手机版是目前手机上最好用的一款地图软件之一,高德地图手机版软件内有着非常精准的定位系统,包括的人的朝向都能够清晰观察到。软件内还有实时路况播报,方便你开车时避开高峰路段,节省路途时间。
1号店app是一款非常好用网上电子商务平台。1号店app可以说是一家网上百货超市,几乎所有日常生活用品等可以在1号店上进行购买,覆盖食品、饮食、进口商品、服饰、鞋帽、清洁用品、数码、家电等非常多的种类。同时1号店拥有独具特色的集成供应链既保证了商品质