-
Notifications
You must be signed in to change notification settings - Fork 9
/
UploadFile.php
152 lines (127 loc) · 3.36 KB
/
UploadFile.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
<?php
namespace Colorizzar;
use Exception;
use RuntimeException;
class UploadFile
{
private $UploadPath = 'uploads';
private $customNameFile;
private $fullPathFile;
/**
* @param string $path Set UploadPath
* @return Colorizzar
*/
public function setUploadPath($path)
{
//var_dump($path);
$this->UploadPath = $path;
return $this;
}
/**
* Get Actual [or default] uploadFile
* @return string
*/
public function getUploadPath()
{
return $this->UploadPath;
}
/**
* @param string $customNameFile
* @return Colorizzar
*/
public function setCustomNameFile($customNameFile)
{
$this->customNameFile = $customNameFile;
return $this;
}
/**
* @return string Colorizzar->customNameFile
*/
public function getCustomNameFile()
{
return $this->customNameFile;
}
public function setFullPathFile($fullPathFile)
{
$this->fullPathFile = $fullPathFile;
}
/**
* @return string Colorizzar->customNameFile
*/
public function getFullPathFile()
{
return $this->fullPathFile;
}
/**
* Generate a Hash
* @return string
*/
public static function generateHash()
{
return strtoupper(bin2hex(openssl_random_pseudo_bytes(8)));
}
/**
* Upload File, just pass array of global $_FILES the core concept is facility to test
* @param array $file with $_FILES to save, should be just one with name="arquivo"
* @param string $customNameFile (optional) custom name
* @return boolean
*/
public function upload(array $file, $customNameFile = '')
{
$this->handle($file);
$name = (($customNameFile == '') ? $file['arquivo']['name'] : $customNameFile);
$tmp_name = $file['arquivo']['tmp_name'];
$this->setCustomNameFile($name);
$upload_dir = $this->UploadPath . DIRECTORY_SEPARATOR . $name;
//var_dump($upload_dir);
//var_dump($name);
$this->setFullPathFile($upload_dir);
//Make my life easy to test, if not upload just copy
if (!is_uploaded_file($tmp_name)) {
return copy($tmp_name, $upload_dir);
//return static::copyImage($tmp_name, $upload_dir);
}
return static::moveUpload($tmp_name, $upload_dir);
}
/**
* Responsible for conditional verification
* @return void
*/
private function handle(array $file)
{
if (empty($file)) {
throw new RuntimeException('File not defined');
}
if (! file_exists($this->UploadPath)) {
mkdir($this->UploadPath, null, true);
}
if ($file['arquivo']['error'] !== UPLOAD_ERR_OK) {
throw new Exception($file['arquivo']['error']);
}
}
/**
* Copy image
* @return bool
*/
protected static function copyImage($tmp_name, $dir)
{
return copy($tmp_name, $dir);
}
/**
* Move image.
* @return bool
*/
protected static function moveUpload($tmp_name, $dir)
{
return move_uploaded_file($tmp_name, $dir);
}
/**
* Return Image Type
* @param string $nameFile
* @return int
*/
public static function getImageType($nameFile)
{
return getimagesize($nameFile)[2];
}
}