-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUploaderComponent.php
More file actions
144 lines (123 loc) · 4.11 KB
/
FileUploaderComponent.php
File metadata and controls
144 lines (123 loc) · 4.11 KB
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
<?php
App::uses('Component', 'Controller');
class FileUploaderComponent extends Component {
protected $file;
protected $dir = false;
protected $maxFileSize = true;
protected $allowedExtensions = true;
protected $newFileName = false;
protected $ext = false;
public function setFile(array $file) {
$this->file = $file;
return $this;
}
public function setUploadDir($dir) {
$this->dir = $dir;
return $this;
}
public function setMaxFileSize($size) {
$this->maxFileSize = $size;
return $this;
}
public function setAllowedExtensions(array $ext) {
$this->allowedExtensions = $ext;
return $this;
}
public function setExt($ext) {
$this->ext = $ext;
}
public function setNewFileName($name) {
$this->newFileName = $name;
}
public function getNewFileName() {
return $this->newFileName;
}
public function upload() {
try {
$this->checkForErrors();
} catch (Exception $e) {
return __($e->getMessage());
}
try {
$this->checkMaxFileSize();
} catch (Exception $e) {
return __($e->getMessage());
}
try {
$this->checkAllowedExtensions();
} catch (Exception $e) {
return __($e->getMessage());
}
if (is_uploaded_file($this->file['tmp_name'])) {
$ext = $this->ext;
if ($this->dir === false) {
$dirname = WWW_ROOT . 'img' . DS;
} else {
rtrim($dirname = $this->dir, DS) . DS;
}
if (!file_exists($dirname)) {
mkdir($dirname, 0777, true);
}
$nfn = $this->filename ? $dirname . $this->filename . '.' . $ext : $dirname . $this->randString(6) . '.' . $ext;
$this->setNewFileName($nfn);
$filePath = $nfn;
move_uploaded_file($this->file['tmp_name'], $filePath);
return true;
}
}
public function deleteIfExists($path) {
// var_dump($path);echo '<br>';
//var_dump(file_exists($path));die;
if (file_exists($path)) {
unlink($path);
}
return $this;
}
protected function checkMaxFileSize() {
$file = $this->file;
$maxFileSize = $this->maxFileSize;
if ($file['size'] > $maxFileSize && $this->maxFileSize !== false)
throw new Exception('Filesize is too large');
return true;
}
protected function checkAllowedExtensions() {
$file = $this->file;
$ext = explode('/', $file['type']);
$ext = $ext[1];
$this->setExt($ext);
if (is_array($this->allowedExtensions)) {
if (in_array($ext, $this->allowedExtensions)) {
throw new Exception('Incorrect extension');
}
}
}
protected function slugify($str) {
$search = array('', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
$replace = array('s', 't', 's', 't', 's', 't', 's', 't', 'i', 'a', 'a', 'i', 'a', 'a', 'e', 'E');
$str = str_ireplace($search, $replace, strtolower(trim($str)));
$str = preg_replace('/[^\w\d\-\ ]/', '', $str);
$str = str_replace(' ', '-', $str);
return preg_replace('/\-{2,}', '-', $str);
}
protected function checkForErrors() {
switch ($this->file['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
throw new RuntimeException('No file sent.');
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
throw new RuntimeException('Exceeded filesize limit.');
default:
throw new RuntimeException('Unknown errors.');
}
}
protected function randString($length, $charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') {
$str = '';
$count = strlen($charset);
while ($length--) {
$str .= $charset[mt_rand(0, $count - 1)];
}
return $str;
}
}