|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Sirius\Upload\Container; |
|
5
|
|
|
|
|
6
|
|
|
class Local implements ContainerInterface |
|
7
|
|
|
{ |
|
8
|
|
|
protected $baseDirectory; |
|
9
|
|
|
|
|
10
|
20 |
|
public function __construct($baseDirectory) |
|
11
|
|
|
{ |
|
12
|
20 |
|
$this->baseDirectory = $this->normalizePath($baseDirectory) . DIRECTORY_SEPARATOR; |
|
13
|
20 |
|
$this->ensureDirectory($this->baseDirectory); |
|
14
|
20 |
|
} |
|
15
|
|
|
|
|
16
|
20 |
|
protected function normalizePath($path) |
|
17
|
|
|
{ |
|
18
|
20 |
|
$path = dirname(rtrim($path, '\\/') . DIRECTORY_SEPARATOR . 'xxx'); |
|
19
|
|
|
|
|
20
|
20 |
|
return rtrim($path, DIRECTORY_SEPARATOR); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
20 |
|
protected function ensureDirectory($directory):bool |
|
24
|
|
|
{ |
|
25
|
20 |
|
if (!file_exists($directory)) { |
|
26
|
7 |
|
mkdir($directory, 0755, true); |
|
27
|
7 |
|
} |
|
28
|
|
|
|
|
29
|
20 |
|
return is_dir($directory) && $this->isWritable(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Check if the container is writable |
|
34
|
|
|
*/ |
|
35
|
20 |
|
public function isWritable():bool |
|
36
|
|
|
{ |
|
37
|
20 |
|
return is_writable($this->baseDirectory); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* This will check if a file is in the container |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $file |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
10 |
|
public function has($file):bool |
|
47
|
|
|
{ |
|
48
|
10 |
|
return $file && file_exists($this->baseDirectory . $file); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Saves the $content string as a file |
|
53
|
|
|
* |
|
54
|
|
|
* @param string $file |
|
55
|
|
|
* @param string $content |
|
56
|
|
|
* @return bool |
|
57
|
|
|
*/ |
|
58
|
11 |
|
public function save($file, $content):bool |
|
59
|
|
|
{ |
|
60
|
11 |
|
$file = $this->normalizePath($file); |
|
61
|
11 |
|
$dir = dirname($this->baseDirectory . $file); |
|
62
|
11 |
|
if ($this->ensureDirectory($dir)) { |
|
63
|
11 |
|
return (bool) file_put_contents($this->baseDirectory . $file, $content); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return false; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Delete the file from the container |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $file |
|
73
|
|
|
* @return bool |
|
74
|
|
|
*/ |
|
75
|
6 |
|
public function delete($file):bool |
|
76
|
|
|
{ |
|
77
|
6 |
|
$file = $this->normalizePath($file); |
|
78
|
6 |
|
if (file_exists($this->baseDirectory . $file)) { |
|
79
|
5 |
|
return unlink($this->baseDirectory . $file); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
2 |
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Moves a temporary uploaded file to a destination in the container |
|
87
|
|
|
* |
|
88
|
|
|
* @param string $localFile local path |
|
89
|
|
|
* @param string $destination |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
11 |
|
public function moveUploadedFile($localFile, $destination):bool |
|
93
|
|
|
{ |
|
94
|
11 |
|
$dir = dirname($this->baseDirectory . $destination); |
|
95
|
11 |
|
if (file_exists($localFile) && $this->ensureDirectory($dir)) { |
|
96
|
|
|
if (is_readable($localFile)) { |
|
97
|
|
|
// rename() would be good but this is better because $localFile may become 'unwritable' |
|
98
|
|
|
$result = copy($localFile, $this->baseDirectory . $destination); |
|
99
|
|
|
@unlink($localFile); |
|
|
|
|
|
|
100
|
10 |
|
return $result; |
|
101
|
|
|
} |
|
102
|
10 |
|
} |
|
103
|
10 |
|
return false; |
|
104
|
10 |
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: