PHP文件上传类
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
97
98
99
100
101
102
103
104
105
106 |
<?php /** PHP 多文件上传类 */ class Upload{ //自定义类型 private
$type ; //自定义大小 private
$size ; //自定义路径 private
$path ; //自定义表单name值 private
$file ; //源文件临时存储位置 private
$tmp_name ; //源文件文件名 private
$orig_file ; //源文件大小 private
$orig_size ; //源文件类型 private
$orig_type ; //上传后新文件名 private
$new_file ; //构造方法初始化 public
function __construct( $type , $size , $path , $file ){ $this ->type = $type ; $this ->size = $size ; $this ->path = $path ; $this ->file = $file ; } //执行文件上传 public
function runing(){ $count
= count ( $_FILES [ $this ->file][ "name" ]); for ( $i =0; $i < $count ; $i ++){ $this ->tmp_name = $_FILES [ $this ->file][ "tmp_name" ][ $i ]; $this ->orig_file = $_FILES [ $this ->file][ "name" ][ $i ]; $this ->orig_size = $_FILES [ $this ->file][ "size" ][ $i ]; $this ->orig_type = strtolower ( substr ( $this ->orig_file, strrpos ( $this ->orig_file, "." )+1)); $this ->new_file = $this ->path. date ( "Y-m-d-H-i-s" ).rand(0,999). "." . $this ->orig_type; if ( $this ->checktype()){ if ( $this ->checksize()){ $this ->fileupload(); } else { echo
"<meta charset=‘utf-8‘>该文件大小错误:" . $this ->orig_file; } } else { echo
"<meta charset=‘utf-8‘>该文件类型错误:" . $this ->orig_file; return
false; } } } //检查文件的类型 private
function checktype(){ if (in_array( $this ->orig_type, $this ->type)){ return
true; } else { return
false; } } //检查文件大小 private
function checksize(){ if ( $this ->orig_size <= $this ->size){ return
true; } else { return
false; } } //执行上传 private
function fileupload(){ if ( is_uploaded_file ( $this ->tmp_name) && isset( $_POST [ "submit" ])){ $file
= move_uploaded_file( $this ->tmp_name, $this ->new_file); } else { return
false; } if ( $file ){ echo
$this ->orig_file. "<meta charset=‘utf-8‘>上传成功!" ; } else { echo
$this ->orig_file. "<meta charset=‘utf-8‘>上传失败!" ; } } } ?> |
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。