-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile-uploader.php
208 lines (182 loc) · 7.49 KB
/
file-uploader.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
<?php
/**
*
* PHP Utilities - File Uploader
* @see https://github.com/andreazorzi/php-utilities
* @author Andrea Zorzi (andreazorzi) <info@zorziandrea.com>
* @version 1.2.0
*
*/
/**
*
* Upload single file on server
*
* @param Array $file $_FILES["file_name"] variable
* @param String $folder The folder where to save the file
* @param Array $settings {
* Additional settings
*
* @param Array $accept File extensions accepted, must start with dot, if not set, it does not accept any file
* @param String $nameformat Pattern to set the new file name, if not set, it keeps the original name
* @param int $iterator Starting iterator number
* }
*
* @return Array An array with the status of the uploaded file and, if the upload was successful, the absolute url of the uploaded file and the new file name {
* 0: The operation was successful
* 1: An error occurred with the uploaded file
* 2: The uploaded file is not an allowed type
* 3: The file was not saved
* }
*
*/
function singleFileUpload(&$file, $folder, $settings = array()){
checkFolder($folder);
if(!isset($settings["iterator"])){
$settings["iterator"] = 0;
}
if(isset($file) && $file["error"] == 0){
$fileinfo = getFileInfo($file["name"]);
$filename = preg_replace('/\s{1,}/i', "_", $file["name"]);
if(isset($settings["accept"]) && !in_array($fileinfo["ext"], $settings["accept"])){
return array("status" => 2, "url" => "");
}
if(isset($settings["nameformat"])){
$filename = $settings["nameformat"];
$filename = str_replace("{filename}", preg_replace('/\s{1,}/i', "_", $fileinfo["name"]), $filename);
$filename = str_replace(".{ext}", $fileinfo["ext"], $filename);
$filename = str_replace("{t}", time(), $filename);
$filename = str_replace("{i}", $settings["iterator"], $filename);
$settings["iterator"] += 1;
}
if(file_put_contents($folder.$filename, file_get_contents($file["tmp_name"])) !== false){
return array("status" => 0, "url" => absolutePath($folder, $filename), "filename" => $filename);
}
return array("status" => 3, "url" => "");
}
return array("status" => 1, "url" => "");
}
/**
*
* Upload miltiple files on server
*
* @param Array $file $_FILES["file_name"] variable
* @param String $folder The folder where to save the file
* @param Array $settings {
* Additional settings
*
* @param Array $accept File extensions accepted, must start with dot, if not set, it does not accept any file
* @param String $nameformat Pattern to set the new file name, if not set, it keeps the original name
* @param int $iterator Starting iterator number
* }
*
* @return Array An array containing for each file the status of the uploaded file and, if the upload was successful, the absolute url of the uploaded file and the new file name {
* 0: The operation was successful
* 1: An error occurred with the uploaded file
* 2: The uploaded file is not an allowed type
* 3: The file was not saved
* }
*
*/
function multipleFileUpload(&$file, $folder, $settings = array()){
checkFolder($folder);
if(!isset($settings["iterator"])){
$settings["iterator"] = 0;
}
$res = array();
for($i = 0; $i < count($file["name"]); $i++){
$status = 0;
if($file["error"][$i] == 0){
$fileinfo = getFileInfo($file["name"][$i]);
$filename = preg_replace('/\s{1,}/i', "_", $file["name"][$i]);
if(isset($settings["accept"]) && !in_array($fileinfo["ext"], $settings["accept"])){
$res[] = array("status" => 2, "url" => "");
}
else{
if(isset($settings["nameformat"])){
$filename = $settings["nameformat"];
$filename = str_replace("{filename}", preg_replace('/\s{1,}/i', "_", $fileinfo["name"]), $filename);
$filename = str_replace(".{ext}", $fileinfo["ext"], $filename);
$filename = str_replace("{t}", time(), $filename);
$filename = str_replace("{i}", $settings["iterator"], $filename);
$settings["iterator"] += 1;
}
if(file_put_contents($folder.$filename, file_get_contents($file["tmp_name"][$i])) !== false){
$res[] = array("status" => 0, "url" => absolutePath($folder, $filename), "filename" => $filename);
}
else{
$res[] = array("status" => 3, "url" => "");
}
}
}
else{
$res[] = array("status" => 1, "url" => "");
}
}
return $res;
}
/**
*
* Check if the passed folder exists, in case the folder is created
*
* @param String $folder The folder path to check
*
*/
function checkFolder($folder){
if (!file_exists($folder)) {
mkdir($folder, 0777, true);
}
}
/**
*
* Get the information of a given file
*
* @param String $filename The path of the file to check
*
* @return Array Array that contains the name and extension of the file
*
*/
function getFileInfo($filename){
$pathinfo = pathinfo($filename);
return array(
"ext" => isset($pathinfo["extension"]) ? ".".mb_strtolower($pathinfo["extension"]) : "",
"name" => isset($pathinfo["filename"]) ? $pathinfo["filename"] : ""
);
}
/**
*
* Generate the absolute path of a file
*
* @param String $folder The folder
* @param String $filename The filename
*
* @return String A string containing the absolute path of the file
*
*/
function absolutePath($folder, $filename){
return str_replace($_SERVER["DOCUMENT_ROOT"], $_SERVER["REQUEST_SCHEME"]."://".$_SERVER["HTTP_HOST"], realpath($folder))."/".$filename;
}
/**
*
* Get the status text from the status code
*
* @param int $status The status code
*
* @return String Status string
*
*/
function getUploadedFileError($status){
if($status == 0){
return "Upload successful";
}
else if($status == 1){
return "Loading error";
}
else if($status == 2){
return "The format of the uploaded file is not allowed";
}
else if($status == 3){
return "The file was not saved on the server";
}
return "";
}
?>