|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ItBlaster\AttachFileBundle\Model; |
|
4
|
|
|
|
|
5
|
|
|
use ItBlaster\AttachFileBundle\Model\om\BaseAttachFile; |
|
6
|
|
|
|
|
7
|
|
|
class AttachFile extends BaseAttachFile |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Удаление файла |
|
11
|
|
|
* |
|
12
|
|
|
* @return bool |
|
13
|
|
|
*/ |
|
14
|
|
|
public function deleteFile() |
|
15
|
|
|
{ |
|
16
|
|
|
$file_path = $this->fullFilePath(); |
|
17
|
|
|
if (is_file($file_path)) { |
|
18
|
|
|
unlink($file_path); |
|
19
|
|
|
} |
|
20
|
|
|
$this |
|
21
|
|
|
->setOriginalName("") |
|
22
|
|
|
->setFileName("") |
|
23
|
|
|
->setExt("") |
|
24
|
|
|
->setSize("") |
|
25
|
|
|
->save(); |
|
26
|
|
|
return true; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Полный путь до папки файла на сервере |
|
31
|
|
|
* |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
|
|
public function fullFilePathDir() |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->getProjectPath().$this->getFilePathDir(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Полный путь файла на сервере |
|
41
|
|
|
* |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
|
|
public function fullFilePath() |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->getProjectPath().$this->getFilePath(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Путь до файла |
|
51
|
|
|
* |
|
52
|
|
|
* /uploads/productmateriali18n/54956796bdc5c.pdf |
|
53
|
|
|
* |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getFilePath() |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->getFilePathDir().$this->getFileName().".".$this->getExt(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Папка, где лежит файл |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function getFilePathDir() |
|
67
|
|
|
{ |
|
68
|
|
|
return "/uploads/".$this->getModel()."/".$this->getObjectId()."/"; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Папка проекта на сервере |
|
73
|
|
|
* |
|
74
|
|
|
* TODO: написать специальный бихейвор для класса AttachFile, |
|
75
|
|
|
* который будет в BaseAttachFile прописывать путь project_path |
|
76
|
|
|
* на основе AppKernel |
|
77
|
|
|
* |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getProjectPath(){ |
|
|
|
|
|
|
81
|
|
|
return $_SERVER["DOCUMENT_ROOT"]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Существует ли файл физически на сервере |
|
86
|
|
|
* |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
|
|
public function issetFile() |
|
90
|
|
|
{ |
|
91
|
|
|
return $this->getFileName() && file_exists($this->fullFilePath()); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Является ли файл изображением |
|
96
|
|
|
* |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
|
|
public function isImage() |
|
100
|
|
|
{ |
|
101
|
|
|
//$_SERVER["DOCUMENT_ROOT"] = '/home/user_name/projects/project_name/web'; |
|
|
|
|
|
|
102
|
|
|
if ($this->issetFile()) { |
|
103
|
|
|
$mime_type = mime_content_type($this->fullFilePath()); |
|
104
|
|
|
if ($mime_type) { |
|
105
|
|
|
$mime_type_params = explode('/', $mime_type); |
|
106
|
|
|
return $mime_type_params[0] == 'image'; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: