|
1
|
|
|
<?php
|
|
2
|
|
|
|
|
3
|
|
|
class Nip_File_Exception extends Exception
|
|
|
|
|
|
|
4
|
|
|
{
|
|
5
|
|
|
}
|
|
6
|
|
|
|
|
7
|
|
|
class Nip_File extends Nip_Object
|
|
|
|
|
|
|
8
|
|
|
{
|
|
9
|
|
|
|
|
10
|
|
|
protected $_path;
|
|
11
|
|
|
protected $_name;
|
|
12
|
|
|
protected $_extension;
|
|
13
|
|
|
|
|
14
|
|
|
public function __construct($path = false)
|
|
15
|
|
|
{
|
|
16
|
|
|
if ($path) {
|
|
17
|
|
|
$this->setPath($path);
|
|
18
|
|
|
}
|
|
19
|
|
|
}
|
|
20
|
|
|
|
|
21
|
|
|
/**
|
|
22
|
|
|
* @param $target
|
|
23
|
|
|
* @return $this
|
|
24
|
|
|
* @throws Nip_File_Exception
|
|
25
|
|
|
*/
|
|
26
|
|
View Code Duplication |
public function move($target)
|
|
|
|
|
|
|
27
|
|
|
{
|
|
28
|
|
|
$dir = dirname($target);
|
|
29
|
|
|
if (!is_dir($target)) {
|
|
30
|
|
|
mkdir($dir, 0755, true);
|
|
31
|
|
|
}
|
|
32
|
|
|
|
|
33
|
|
|
if (rename($this->getPath(), $target)) {
|
|
34
|
|
|
$this->setPath($target);
|
|
35
|
|
|
} else {
|
|
36
|
|
|
throw new Nip_File_Exception("Cannot move $this->_path file to $target");
|
|
37
|
|
|
}
|
|
38
|
|
|
return $this;
|
|
39
|
|
|
}
|
|
40
|
|
|
|
|
41
|
|
|
public function getPath()
|
|
42
|
|
|
{
|
|
43
|
|
|
return $this->_path;
|
|
44
|
|
|
}
|
|
45
|
|
|
|
|
46
|
|
|
public function setPath($path)
|
|
47
|
|
|
{
|
|
48
|
|
|
$this->_name = basename($path);
|
|
49
|
|
|
$this->_extension = pathinfo($path, PATHINFO_EXTENSION);
|
|
50
|
|
|
$this->_path = $path;
|
|
51
|
|
|
return $this;
|
|
52
|
|
|
}
|
|
53
|
|
|
|
|
54
|
|
|
/**
|
|
55
|
|
|
* @param $target
|
|
56
|
|
|
* @return $this
|
|
57
|
|
|
* @throws Nip_File_Exception
|
|
58
|
|
|
*/
|
|
59
|
|
View Code Duplication |
public function copy($target)
|
|
|
|
|
|
|
60
|
|
|
{
|
|
61
|
|
|
$dir = dirname($target);
|
|
62
|
|
|
if (!is_dir($target)) {
|
|
63
|
|
|
mkdir($dir, 0755, true);
|
|
64
|
|
|
}
|
|
65
|
|
|
|
|
66
|
|
|
if (copy($this->getPath(), $target)) {
|
|
67
|
|
|
$this->setPath($target);
|
|
68
|
|
|
} else {
|
|
69
|
|
|
throw new Nip_File_Exception("Cannot copy $this->_path file to $target");
|
|
70
|
|
|
}
|
|
71
|
|
|
return $this;
|
|
72
|
|
|
}
|
|
73
|
|
|
|
|
74
|
|
|
/**
|
|
75
|
|
|
* @param string $target
|
|
76
|
|
|
* @return Nip_Process
|
|
77
|
|
|
*/
|
|
78
|
|
View Code Duplication |
public function unzip($target)
|
|
|
|
|
|
|
79
|
|
|
{
|
|
80
|
|
|
if (!is_dir($target)) {
|
|
81
|
|
|
mkdir($target, 0755, true);
|
|
82
|
|
|
}
|
|
83
|
|
|
|
|
84
|
|
|
$process = new Nip_Process("unzip {$this->_path} -d $target");
|
|
85
|
|
|
$process->run();
|
|
86
|
|
|
|
|
87
|
|
|
return $process;
|
|
88
|
|
|
}
|
|
89
|
|
|
|
|
90
|
|
|
public function download($filename = false, $contentType = false)
|
|
91
|
|
|
{
|
|
92
|
|
|
if (!$filename) {
|
|
93
|
|
|
$filename = $this->getName();
|
|
94
|
|
|
}
|
|
95
|
|
|
if (!$contentType) {
|
|
96
|
|
|
$contentType = "application/force-download";
|
|
97
|
|
|
}
|
|
98
|
|
|
|
|
99
|
|
|
header("Pragma: public");
|
|
100
|
|
|
header("Expires: 0");
|
|
101
|
|
|
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
|
|
102
|
|
|
header("Cache-Control: private", false);
|
|
103
|
|
|
header("Content-Type: $contentType");
|
|
104
|
|
|
header("Content-Disposition: attachment; filename=\"$filename\"");
|
|
105
|
|
|
header("Content-Length: {$this->getSize()}");
|
|
106
|
|
|
header("Content-Transfer-Encoding: binary");
|
|
107
|
|
|
|
|
108
|
|
|
readfile($this->getPath());
|
|
109
|
|
|
exit();
|
|
|
|
|
|
|
110
|
|
|
}
|
|
111
|
|
|
|
|
112
|
|
|
public function getName()
|
|
113
|
|
|
{
|
|
114
|
|
|
return $this->_name;
|
|
115
|
|
|
}
|
|
116
|
|
|
|
|
117
|
|
|
public function getSize()
|
|
118
|
|
|
{
|
|
119
|
|
|
return filesize($this->getPath());
|
|
120
|
|
|
}
|
|
121
|
|
|
|
|
122
|
|
|
public function delete()
|
|
123
|
|
|
{
|
|
124
|
|
|
unlink($this->getPath());
|
|
125
|
|
|
}
|
|
126
|
|
|
|
|
127
|
|
|
public function getExtension()
|
|
128
|
|
|
{
|
|
129
|
|
|
return $this->_extension;
|
|
130
|
|
|
}
|
|
131
|
|
|
|
|
132
|
|
|
public function getTime()
|
|
133
|
|
|
{
|
|
134
|
|
|
return filemtime($this->getPath());
|
|
135
|
|
|
}
|
|
136
|
|
|
|
|
137
|
|
|
public function getMimeType()
|
|
138
|
|
|
{
|
|
139
|
|
|
if (function_exists('mime_content_type')) {
|
|
140
|
|
|
return mime_content_type($this->getPath());
|
|
141
|
|
|
|
|
142
|
|
|
} elseif (function_exists('finfo_open')) {
|
|
143
|
|
|
$finfo = finfo_open(FILEINFO_MIME);
|
|
144
|
|
|
$mimetype = finfo_file($finfo, $this->getPath());
|
|
145
|
|
|
finfo_close($finfo);
|
|
146
|
|
|
return $mimetype;
|
|
147
|
|
|
}
|
|
148
|
|
|
|
|
149
|
|
|
return "unknown";
|
|
150
|
|
|
}
|
|
151
|
|
|
|
|
152
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.