|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Bluz PHP Team |
|
4
|
|
|
* @link https://github.com/bluzphp/skeleton |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @namespace |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace Application\Media; |
|
11
|
|
|
|
|
12
|
|
|
use Application\Exception; |
|
13
|
|
|
use Bluz\Proxy\Request; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class Crud of Media |
|
17
|
|
|
* @package Application\Media |
|
18
|
|
|
* |
|
19
|
|
|
* @method Table getTable() |
|
20
|
|
|
*/ |
|
21
|
|
|
class Crud extends \Bluz\Crud\Table |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $uploadDir; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* createOne |
|
30
|
|
|
* |
|
31
|
|
|
* @param array $data |
|
32
|
|
|
* @throws \Application\Exception |
|
33
|
|
|
* @return integer |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public function createOne($data) |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* Process HTTP File |
|
39
|
|
|
*/ |
|
40
|
1 |
|
$file = Request::getFile('file'); |
|
41
|
|
|
|
|
42
|
1 |
|
if (!$file or $file->getError() != UPLOAD_ERR_OK) { |
|
|
|
|
|
|
43
|
|
|
if ($file->getError() == UPLOAD_ERR_NO_FILE) { |
|
44
|
|
|
throw new Exception("Please choose file for upload"); |
|
45
|
|
|
} |
|
46
|
|
|
throw new Exception("Sorry, I can't receive file"); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Generate image name |
|
51
|
|
|
*/ |
|
52
|
1 |
|
$pathinfo = pathinfo($file->getClientFilename()); |
|
53
|
|
|
|
|
54
|
1 |
|
$fileName = strtolower(isset($data['title'])?$data['title']:$pathinfo['filename']); |
|
55
|
|
|
|
|
56
|
|
|
// Prepare filename |
|
57
|
1 |
|
$fileName = preg_replace('/[ _;:]+/i', '-', $fileName); |
|
58
|
1 |
|
$fileName = preg_replace('/[-]+/i', '-', $fileName); |
|
59
|
1 |
|
$fileName = preg_replace('/[^a-z0-9.-]+/i', '', $fileName); |
|
60
|
|
|
|
|
61
|
|
|
// If name is wrong |
|
62
|
1 |
|
if (empty($fileName)) { |
|
63
|
|
|
$fileName = date('Y-m-d-His'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// If file already exists, increment name |
|
67
|
1 |
|
$originFileName = $fileName; |
|
68
|
1 |
|
$counter = 0; |
|
69
|
|
|
|
|
70
|
1 |
|
while (file_exists($this->uploadDir .'/'. $fileName .'.'. $pathinfo['extension'])) { |
|
71
|
|
|
$counter++; |
|
72
|
|
|
$fileName = $originFileName .'-'. $counter; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
$filePath = $this->uploadDir .'/'. $fileName .'.'. $pathinfo['extension']; |
|
76
|
|
|
|
|
77
|
|
|
// Setup new name and move to user directory |
|
78
|
1 |
|
$file->moveTo($filePath); |
|
79
|
|
|
|
|
80
|
1 |
|
$publicDir = substr($this->uploadDir, strlen(PATH_PUBLIC) + 1); |
|
81
|
|
|
|
|
82
|
1 |
|
$data['file'] = $publicDir .'/'. $fileName .'.'. $pathinfo['extension']; |
|
83
|
1 |
|
$data['type'] = $file->getClientMediaType(); |
|
84
|
|
|
|
|
85
|
1 |
|
$row = $this->getTable()->create(); |
|
86
|
|
|
|
|
87
|
1 |
|
$row->setFromArray($data); |
|
88
|
1 |
|
return $row->save(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* setUploadDir |
|
93
|
|
|
* |
|
94
|
|
|
* @param $directory |
|
95
|
|
|
* @throws Exception |
|
96
|
|
|
* @return self |
|
97
|
|
|
*/ |
|
98
|
3 |
|
public function setUploadDir($directory) |
|
99
|
|
|
{ |
|
100
|
3 |
|
if (!is_dir($directory) && !@mkdir($directory, 0755, true)) { |
|
101
|
|
|
throw new Exception("Upload folder is not exists and I can't create it"); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
3 |
|
if (!is_writable($directory)) { |
|
105
|
|
|
throw new Exception("Upload folder is not writable"); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
3 |
|
$this->uploadDir = $directory; |
|
109
|
|
|
|
|
110
|
3 |
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.