1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
if (class_exists('MongoGridFSFile', false)) { |
17
|
|
|
return; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
class MongoGridFSFile |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @link http://php.net/manual/en/class.mongogridfsfile.php#mongogridfsfile.props.file |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
public $file; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @link http://php.net/manual/en/class.mongogridfsfile.php#mongogridfsfile.props.gridfs |
30
|
|
|
* @var $gridfs |
31
|
|
|
*/ |
32
|
|
|
protected $gridfs; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @link http://php.net/manual/en/mongogridfsfile.construct.php |
36
|
|
|
* |
37
|
|
|
* @param MongoGridFS $gridfs The parent MongoGridFS instance |
38
|
|
|
* @param array $file A file from the database |
39
|
|
|
*/ |
40
|
|
|
public function __construct(MongoGridFS $gridfs, array $file) |
41
|
|
|
{ |
42
|
|
|
$this->gridfs = $gridfs; |
43
|
|
|
$this->file = $file; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns this file's filename |
48
|
|
|
* @link http://php.net/manual/en/mongogridfsfile.getfilename.php |
49
|
|
|
* @return string Returns the filename |
50
|
|
|
*/ |
51
|
|
|
public function getFilename() |
52
|
|
|
{ |
53
|
|
|
return isset($this->file['filename']) ? $this->file['filename'] : null; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns this file's size |
58
|
|
|
* @link http://php.net/manual/en/mongogridfsfile.getsize.php |
59
|
|
|
* @return int Returns this file's size |
60
|
|
|
*/ |
61
|
|
|
public function getSize() |
62
|
|
|
{ |
63
|
|
|
return $this->file['length']; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Writes this file to the filesystem |
68
|
|
|
* @link http://php.net/manual/en/mongogridfsfile.write.php |
69
|
|
|
* @param string $filename The location to which to write the file (path+filename+extension). If none is given, the stored filename will be used. |
70
|
|
|
* @return int Returns the number of bytes written |
71
|
|
|
*/ |
72
|
|
|
public function write($filename = null) |
73
|
|
|
{ |
74
|
|
|
if ($filename === null) { |
75
|
|
|
$filename = $this->getFilename(); |
76
|
|
|
} |
77
|
|
|
if (empty($filename)) { |
78
|
|
|
$filename = 'file'; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (! $handle = fopen($filename, 'w')) { |
82
|
|
|
trigger_error('Can not open the destination file', E_USER_ERROR); |
83
|
|
|
return 0; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$written = $this->copyToResource($handle); |
87
|
|
|
fclose($handle); |
88
|
|
|
|
89
|
|
|
return $written; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* This will load the file into memory. If the file is bigger than your memory, this will cause problems! |
94
|
|
|
* @link http://php.net/manual/en/mongogridfsfile.getbytes.php |
95
|
|
|
* @return string Returns a string of the bytes in the file |
96
|
|
|
*/ |
97
|
|
|
public function getBytes() |
98
|
|
|
{ |
99
|
|
|
$result = ''; |
100
|
|
|
foreach ($this->getChunks() as $chunk) { |
101
|
|
|
$result .= $chunk['data']->bin; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $result; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* This method returns a stream resource that can be used to read the stored file with all file functions in PHP. |
109
|
|
|
* The contents of the file are pulled out of MongoDB on the fly, so that the whole file does not have to be loaded into memory first. |
110
|
|
|
* At most two GridFSFile chunks will be loaded in memory. |
111
|
|
|
* |
112
|
|
|
* @link http://php.net/manual/en/mongogridfsfile.getresource.php |
113
|
|
|
* @return resource Returns a resource that can be used to read the file with |
114
|
|
|
*/ |
115
|
|
|
public function getResource() |
116
|
|
|
{ |
117
|
|
|
$handle = fopen('php://temp', 'w+'); |
118
|
|
|
$this->copyToResource($handle); |
119
|
|
|
rewind($handle); |
120
|
|
|
|
121
|
|
|
return $handle; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
private function copyToResource($handle) |
125
|
|
|
{ |
126
|
|
|
$written = 0; |
127
|
|
|
foreach ($this->getChunks() as $chunk) { |
128
|
|
|
$written += fwrite($handle, $chunk['data']->bin); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $written; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
private function getChunks() |
135
|
|
|
{ |
136
|
|
|
return $chunks = $this->gridfs->chunks->find( |
|
|
|
|
137
|
|
|
['files_id' => $this->file['_id']], |
138
|
|
|
['data' => 1], |
139
|
|
|
['n' => 1] |
|
|
|
|
140
|
|
|
); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.