|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BigFileTools; |
|
4
|
|
|
|
|
5
|
|
|
use BigFileTools\Driver\ISizeDriver; |
|
6
|
|
|
|
|
7
|
|
|
class File |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Full path to file |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
private $path; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var ISizeDriver |
|
17
|
|
|
*/ |
|
18
|
|
|
private $sizeDriver; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* File constructor. |
|
22
|
|
|
* @param string $absoluteFilePath **absolute** path to file |
|
23
|
|
|
* @param ISizeDriver $sizeDriver Driver used for determining file size |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct($absoluteFilePath, ISizeDriver $sizeDriver) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->path = $absoluteFilePath; |
|
28
|
|
|
$this->sizeDriver = $sizeDriver; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return \Brick\Math\BigInteger |
|
33
|
|
|
* @throws Driver\Exception |
|
34
|
|
|
*/ |
|
35
|
|
|
public function getSize() |
|
36
|
|
|
{ |
|
37
|
|
|
return $this->sizeDriver->getFileSize($this->path); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Absolute path to file |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getPath() |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->path; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Returns PHP's file info (some values can be incorrect due to platform limitations) |
|
51
|
|
|
* @return \SplFileInfo |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getFileInfo() |
|
54
|
|
|
{ |
|
55
|
|
|
return new \SplFileInfo($this->path); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Returns if file exists and is readable |
|
60
|
|
|
* @return bool |
|
61
|
|
|
*/ |
|
62
|
|
|
public function isReadableFile() |
|
63
|
|
|
{ |
|
64
|
|
|
// Do not use is_file |
|
65
|
|
|
// @link https://bugs.php.net/bug.php?id=27792 |
|
66
|
|
|
// $readable = is_readable($file); // does not always return correct value for directories |
|
67
|
|
|
|
|
68
|
|
|
$fp = @fopen($this->path, "r"); // must be file and must be readable |
|
69
|
|
|
if($fp) { |
|
70
|
|
|
fclose($fp); |
|
71
|
|
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
return false; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Moves file to new location / rename |
|
78
|
|
|
* @param string $destination path |
|
79
|
|
|
* @return bool |
|
80
|
|
|
* @throws Exception |
|
81
|
|
|
*/ |
|
82
|
|
|
function move($destination) |
|
83
|
|
|
{ |
|
84
|
|
|
if (move_uploaded_file($this->path, $destination)) { |
|
85
|
|
|
$this->path = $destination; |
|
86
|
|
|
return; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
@unlink($destination); // needed in PHP < 5.3 & Windows; intentionally @ |
|
|
|
|
|
|
90
|
|
|
if (rename($this->path, $destination)) { |
|
91
|
|
|
$this->path = $destination; |
|
92
|
|
|
return; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
if (copy($this->path, $destination)) { |
|
96
|
|
|
unlink($this->path); // delete file |
|
97
|
|
|
$this->path = $destination; |
|
98
|
|
|
return; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
throw new Exception("File cannot be moved. All supported methods failed."); |
|
102
|
|
|
} |
|
103
|
|
|
} |
If you suppress an error, we recommend checking for the error condition explicitly: