-
Notifications
You must be signed in to change notification settings - Fork 27
/
Local.php
executable file
·88 lines (72 loc) · 2.31 KB
/
Local.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
<?php
declare(strict_types=1);
namespace Sirius\Upload\Container;
class Local implements ContainerInterface
{
protected string $baseDirectory;
public function __construct(string $baseDirectory)
{
$this->baseDirectory = $this->normalizePath($baseDirectory) . DIRECTORY_SEPARATOR;
$this->ensureDirectory($this->baseDirectory);
}
protected function normalizePath(string $path): string
{
$path = dirname(rtrim($path, '\\/') . DIRECTORY_SEPARATOR . 'xxx');
return rtrim($path, DIRECTORY_SEPARATOR);
}
protected function ensureDirectory(string $directory): bool
{
if ( ! is_dir($directory)) {
mkdir($directory, 0755, true);
}
return is_dir($directory) && $this->isWritable();
}
/**
* Check if the container is writable
*/
public function isWritable(): bool
{
return is_writable($this->baseDirectory);
}
/**
* This will check if a file is in the container
*
* @param string $file
*
* @return bool
*/
public function has(string $file): bool
{
return $file && file_exists($this->baseDirectory . $file);
}
public function save(string $file, string $content): bool
{
$file = $this->normalizePath($file);
$dir = dirname($this->baseDirectory . $file);
if ($this->ensureDirectory($dir)) {
return (bool)file_put_contents($this->baseDirectory . $file, $content);
}
return false;
}
public function delete(string $file): bool
{
$file = $this->normalizePath($file);
if (file_exists($this->baseDirectory . $file)) {
return unlink($this->baseDirectory . $file);
}
return true;
}
public function moveUploadedFile(string $localFile, string $destination): bool
{
$dir = dirname($this->baseDirectory . $destination);
if (file_exists($localFile) && $this->ensureDirectory($dir)) {
if (is_readable($localFile)) {
// rename() would be good but this is better because $localFile may become 'unwritable'
$result = copy($localFile, $this->baseDirectory . $destination);
@unlink($localFile);
return $result;
}
}
return false;
}
}