forked from mazharul/Uploaded-file-manipulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MazUploadClass.php
229 lines (180 loc) · 6.72 KB
/
MazUploadClass.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/**
* php class for validating any picutre, video, files or script
* and creating valid path on the server to upload the file
* @author Mazharul Anwar <[email protected]>
* @Homepage <strong>http://protishobdo.com</strong>
* @Author Twitter <strong>http://twitter.com/mazharul_anwar</strong>
* @package uploadClass
* @version v1.0.200
* @Previous version v1.0.199
* @release date: April28, 2010
* @versionChange date May 11, 2010
* @Thanks to Farhan <[email protected]>
* @copyright Copyright (C) 2010, Mazharul Anwar
* @license http://www.gnu.org/licenses/ GNU General Public License
*/
/**
Copyright (C) 2010 Mazharul Anwar([email protected])
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* main function to upload image
* in the server
*/
class MazUploadClass{
/**
* @var array
*/
public $errorMsg;
public $mainArray = array();
/**
* Creating Path for uploaded files
* default path is ..user_uploads/$uploadType/$tyear/$tmonth/$tday/
* you can change the $uploadType from your file handeling php file
*/
public function createPath($uploadType){
$tyear = date("Y");
$tmonth = date("m");
$tday = date("d");
/**
* Checking the directory and creating folders for images according to the year, month and day
*/
if (!is_dir("user_uploads/$uploadType/$tyear/$tmonth/$tday"))
{
if (!is_dir("user_uploads/$uploadType/$tyear/$tmonth"))
{
if (!is_dir("user_uploads/$uploadType/$tyear"))
{
if(!is_dir("user_uploads/$uploadType"))
{
if(!is_dir("user_uploads"))
{
$umask = umask(0);
mkdir("user_uploads", 0777, true);
umask($umask);
}else{}
$umask = umask(0);
mkdir("user_uploads/$uploadType", 0777, true);
umask($umask);
}else{}
$umask = umask(0);
mkdir("user_uploads/$uploadType/$tyear", 0777, true);
umask($umask);
} else {}
$umask = umask(0);
mkdir("user_uploads/$uploadType/$tyear/$tmonth", 0777, true);
umask($umask);
}
else {}
$umask = umask(0);
mkdir("user_uploads/$uploadType/$tyear/$tmonth/$tday", 0777, true);
umask($umask);
$path = "user_uploads/$uploadType/$tyear/$tmonth/$tday/";
}
else {}
return $this->path = "user_uploads/$uploadType/$tyear/$tmonth/$tday/";
}
/**
* end of function createPath
*/
/**
* uploaded file validation
* @param $pdf, the name of uploaded file, eg. $pdf = $_FILES['pdf'];
* @param $size the file size that you want to restrict the user
* @param $fltype is the type of file, currently support pdf, image, video, audio, pdf files!
*/
public function uploadPDF($pdf, $size, $fltype){
if($fltype != ""){ #start of pdf file upload
$this->arrayCondition($fltype);
if(!empty($pdf)){ #checking file validation
$mimeTypeFlag = "yes";
foreach($this->mainArray['MIMEtype'] as $r){
if($pdf['type'] == $r){
$mimeTypeFlag = "no";
break;
}
}
if(($mimeTypeFlag == "no") && ($pdf['size'] <= $size)){
$ext = end(explode(".",strtolower($pdf['name'])));
$this->errorFlag['fileFlag'] = "yes";
foreach($this->mainArray['fileTypeArray'] as $value){
if($ext == $value){
$this->errorFlag['fileFlag'] = "no";
break;
}else{ }
}
}else{
$errorPdf = "yes";
$this->errorFlag['fileFlag'] = "yes";
}
}else{
$this->errorFlag['fileFlag'] = "yes";
}
}else{
$this->errorFlag['fileFlag'] = "yes";
$this->msgError['msgError'] = "FileType is not provided";
}#--/end of file validation
return $this->errorFlag;
} #---/end of pdf file upload-----
/**
* selecting array
* @return string
*/
private function arrayCondition($fltype){
if($fltype == "pdf"){
$fileTypeArray = array('pdf','x-pdf', 'acrobat', 'vnd.pdf' );
$MIMEtype = array('application/pdf','application/x-pdf', 'application/acrobat', 'applications/vnd.pdf', 'text/pdf', 'text/x-pdf');
$this->mainArray['fileTypeArray']= $fileTypeArray;
$this->mainArray['MIMEtype'] = $MIMEtype;
}elseif($fltype == "image"){
$fileTypeArray = array('jpg', 'jpeg', 'gif', 'png', 'tif', 'bmp');
$MIMEtype = array('image/jpg', 'image/gif', 'image/jpeg', 'image/png', 'image/tiff', 'image/bmp');
$this->mainArray['fileTypeArray'] = $fileTypeArray;
$this->mainArray['MIMEtype'] = $MIMEtype;
}
/*elseif($filetype == "file"){
$fileTypeArray = array('php', 'php5', 'html', 'css', 'aspx', 'asp');
}*/elseif($fltype == "video"){
$fileTypeArray = array('map2', 'mpa', 'mpe', 'mpeg', 'mpg', 'mpeg', 'mov', 'qt', 'avi', 'movie', 'flv', 'swf', 'mp4', 'moov');
$MIMEtype = array('video/mpeg', 'video/quicktime', 'video/x-msvideo', 'video/x-sgi-movie', 'video/avi', 'video/x-flv', 'video/mp4');
$this->mainArray['fileTypeArray']= $fileTypeArray;
$this->mainArray['MIMEtype'] = $MIMEtype;
}elseif($fltype == "audio"){
$fileTypeArray = array('mp2', 'mp3', 'snd', 'wav', 'tif', 'aif', 'aifc', 'au', 'mid', 'midi', 'mod');
$MIMEtype = array('audio/basic', 'audio/x-wav', 'audio/aiff', 'audio/x-aiff', 'audio/midi', 'audio/x-midi', 'audio/mod', 'audio/x-mod');
$this->mainArray['fileTypeArray']= $fileTypeArray;
$this->mainArray['MIMEtype'] = $MIMEtype;
}
}#----/end of Array select
/**
* Generating random code
* @param string $randomDigit
* @return string
*/
public function randomCode(){
$this->randomDigit=mt_rand();
return $this->randomDigit;
}
/**
* upload file
* @param $file is temporary file name , $_FILES['pdf']['tmp_name']
* @param original path to the server to store the file
*/
public function uploadToServer($file, $to){
if(!empty($file) && !empty($to)){
if(move_uploaded_file($file, $to))
return true;
}
}
} #----/end of class----
?>