1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: onysko |
5
|
|
|
* Date: 17.11.2014 |
6
|
|
|
* Time: 14:12 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace samsonphp\fs; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Local file system adapter implementation |
13
|
|
|
* @package samson\upload |
14
|
|
|
*/ |
15
|
|
|
class LocalFileService extends AbstractFileService |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Write data to a specific relative location |
19
|
|
|
* |
20
|
|
|
* @param mixed $data Data to be written |
21
|
|
|
* @param string $filename File name |
22
|
|
|
* @param string $uploadDir Relative file path |
23
|
|
|
* @return string|false Relative path to created file, false if there were errors |
24
|
|
|
*/ |
25
|
|
|
public function write($data, $filename = '', $uploadDir = '') |
26
|
|
|
{ |
27
|
|
|
// Build path to writing file |
28
|
|
|
$path = $uploadDir.'/'.$filename; |
29
|
|
|
|
30
|
|
|
// Put file and return true if at least one byte is written |
31
|
|
|
if (file_put_contents($path, $data) !== false) { |
32
|
|
|
return $uploadDir.'/'; |
33
|
|
|
} else { // We have failed my lord.. |
34
|
|
|
return false; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Check existing current file in current file system |
40
|
|
|
* @param $filename string Filename |
41
|
|
|
* @return boolean File exists or not |
42
|
|
|
*/ |
43
|
|
|
public function exists($filename) |
44
|
|
|
{ |
45
|
|
|
return file_exists($filename); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Read the file from current file system |
50
|
|
|
* @param $filePath string Path to file |
51
|
|
|
* @param $filename string |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function read($filePath, $filename = null) |
55
|
|
|
{ |
56
|
|
|
return file_get_contents($filePath); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Delete file from current file system |
61
|
|
|
* @param $filename string File for deleting |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
|
|
public function delete($filename) |
65
|
|
|
{ |
66
|
|
|
unlink($filename); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get file extension in current file system |
71
|
|
|
* @param $filePath string Path |
72
|
|
|
* @return string|bool false if extension not found, otherwise file extension |
73
|
|
|
*/ |
74
|
|
|
public function extension($filePath) |
75
|
|
|
{ |
76
|
|
|
return pathinfo($filePath, PATHINFO_EXTENSION); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Define if $filePath is directory |
81
|
|
|
* @param string $filePath Path |
82
|
|
|
* @return boolean Is $path a directory or not |
83
|
|
|
*/ |
84
|
|
|
public function isDir($filePath) |
85
|
|
|
{ |
86
|
|
|
return is_dir($filePath); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get all entries in $path |
91
|
|
|
* @param string $path Folfer path for listing |
92
|
|
|
* @return array Collection of entries int folder |
93
|
|
|
*/ |
94
|
|
|
protected function directoryFiles($path) |
95
|
|
|
{ |
96
|
|
|
$result = array(); |
97
|
|
|
|
98
|
|
|
// Get all entries in path |
99
|
|
|
foreach (array_diff(scandir($path), array('..', '.')) as $entry) { |
100
|
|
|
// Build full REAL path to entry |
101
|
|
|
$result[] = realpath($path . '/' . $entry); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $result; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get recursive $path listing collection |
109
|
|
|
* @param string $path Path for listing contents |
110
|
|
|
* @param array $restrict Collection of restricted paths |
111
|
|
|
* @param array $result Collection of restricted paths |
112
|
|
|
* @return array $path recursive directory listing |
113
|
|
|
*/ |
114
|
|
|
public function dir($path, $restrict = array(), & $result = array()) |
115
|
|
|
{ |
116
|
|
|
// Check if we can read this path |
117
|
|
|
foreach ($this->directoryFiles($path) as $fullPath) { |
118
|
|
|
// If this is a file |
119
|
|
|
if (!$this->isDir($fullPath)) { |
120
|
|
|
$result[] = $fullPath; |
121
|
|
|
} elseif (in_array($fullPath, $restrict) === false) { |
122
|
|
|
// Check if this folder is not in ignored list |
123
|
|
|
// If this is a folder - go deeper in recursion |
124
|
|
|
$this->dir($fullPath, $restrict, $result); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
// Sort results |
129
|
|
|
sort($result); |
130
|
|
|
|
131
|
|
|
return $result; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Create catalog in selected location |
136
|
|
|
* @param string $path Path for new catalog |
137
|
|
|
* @return boolean Result of catalog creating |
138
|
|
|
*/ |
139
|
|
|
public function mkDir($path) |
140
|
|
|
{ |
141
|
|
|
if (!file_exists($path)) { |
142
|
|
|
mkdir($path, 0775, true); |
143
|
|
|
return true; |
144
|
|
|
} |
145
|
|
|
return false; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|