forked from AmericanCouncils/Transcoding
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.php
More file actions
Latest commit
110 lines (91 loc) · 2.69 KB
/
Copy pathFile.php
File metadata and controls
110 lines (91 loc) · 2.69 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
namespaceAC\Component\Transcoding;
/**
* An extension of SplFileObject, File instances are used as input/output for the Transcoder. They mostly extend
* the base file object with convenience methods for mime checking (which requires the fileinfo PHP extension)
*
* @package Transcoding
* @author Evan Villemez
*/
class File extends \SplFileObject
{
private$_realpath = false;
private$_finfo_mime_type = false;
private$_finfo_mime_encoding = false;
private$_finfo_mime = false;
publicfunction__construct($path)
{
parent::__construct($path);
$this->_realpath = realpath($path);
}
publicfunctiongetType()
{
return$this->isDir() ? 'directory' : 'file';
}
publicfunctiongetContents()
{
returnfile_get_contents($this->_realpath);
}
publicfunctionputContents($content)
{
returnfile_put_contents($this->_realpath, $content);
}
/**
* Returns an array of contained file objects if this file is a directory, otherwise false
*
* Note that directory links (`.` and `..`) are always ignored
*
* @return array | false
*/
publicfunctiongetContainedFiles()
{
if ($this->isDir()) {
$files = array();
$basePath = rtrim($this->_realpath, DIRECTORY_SEPARATOR);
foreach (scandir($this->_realpath) as$fileName) {
if (!in_array($fileName, array('.','..'))) {
$files[] = newFile($basePath.DIRECTORY_SEPARATOR.$fileName);
}
}
return$files;
}
returnfalse;
}
publicfunctiongetExtension()
{
returnpathinfo($this->getFilename(), PATHINFO_EXTENSION);
}
publicfunctiongetMimeType()
{
return$this->getFinfoMimeType()->file($this->_realpath);
}
publicfunctiongetMimeEncoding()
{
return$this->getFinfoMimeEncoding()->file($this->_realpath);
}
publicfunctiongetMime()
{
return$this->getFinfoMime()->file($this->_realpath);
}
privatefunctiongetFinfoMime()
{
if (!$this->_finfo_mime) {
$this->_finfo_mime = new \finfo(FILEINFO_MIME);
}
return$this->_finfo_mime;
}
privatefunctiongetFinfoMimeType()
{
if (!$this->_finfo_mime_type) {
$this->_finfo_mime_type = new \finfo(FILEINFO_MIME_TYPE);
}
return$this->_finfo_mime_type;
}
privatefunctiongetFinfoMimeEncoding()
{
if (!$this->_finfo_mime_encoding) {
$this->_finfo_mime_encoding = new \finfo(FILEINFO_MIME_ENCODING);
}
return$this->_finfo_mime_encoding;
}
}