| 1 | <?php |
||
| 16 | class MongoGridFSFile |
||
|
1 ignored issue
–
show
|
|||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @link http://php.net/manual/en/class.mongogridfsfile.php#mongogridfsfile.props.file |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | public $file; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @link http://php.net/manual/en/class.mongogridfsfile.php#mongogridfsfile.props.gridfs |
||
| 26 | * @var $gridfs |
||
| 27 | */ |
||
| 28 | protected $gridfs; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @link http://php.net/manual/en/mongogridfsfile.construct.php |
||
| 32 | * |
||
| 33 | * @param MongoGridFS $gridfs The parent MongoGridFS instance |
||
| 34 | * @param array $file A file from the database |
||
| 35 | * @return MongoGridFSFile Returns a new MongoGridFSFile |
||
| 36 | */ |
||
| 37 | public function __construct(MongoGridFS $gridfs, array $file) |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Returns this file's filename |
||
| 45 | * @link http://php.net/manual/en/mongogridfsfile.getfilename.php |
||
| 46 | * @return string Returns the filename |
||
| 47 | */ |
||
| 48 | public function getFilename() |
||
| 49 | { |
||
| 50 | return isset($this->file['filename']) ? $this->file['filename'] : null; |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Returns this file's size |
||
| 55 | * @link http://php.net/manual/en/mongogridfsfile.getsize.php |
||
| 56 | * @return int Returns this file's size |
||
| 57 | */ |
||
| 58 | public function getSize() |
||
| 59 | { |
||
| 60 | return $this->file['length']; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Writes this file to the filesystem |
||
| 65 | * @link http://php.net/manual/en/mongogridfsfile.write.php |
||
| 66 | * @param string $filename The location to which to write the file (path+filename+extension). If none is given, the stored filename will be used. |
||
| 67 | * @return int Returns the number of bytes written |
||
| 68 | */ |
||
| 69 | public function write($filename = null) |
||
| 70 | { |
||
| 71 | if ($filename === null) { |
||
| 72 | $filename = $this->getFilename(); |
||
| 73 | } |
||
| 74 | if (empty($filename)) { |
||
| 75 | $filename = 'file'; |
||
| 76 | } |
||
| 77 | |||
| 78 | if (! $handle = fopen($filename, 'w')) { |
||
| 79 | trigger_error(E_ERROR, 'Can not open the destination file'); |
||
| 80 | return 0; |
||
| 81 | } |
||
| 82 | |||
| 83 | $written = $this->copyToResource($handle); |
||
| 84 | fclose($handle); |
||
| 85 | |||
| 86 | return $written; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * This will load the file into memory. If the file is bigger than your memory, this will cause problems! |
||
| 91 | * @link http://php.net/manual/en/mongogridfsfile.getbytes.php |
||
| 92 | * @return string Returns a string of the bytes in the file |
||
| 93 | */ |
||
| 94 | public function getBytes() |
||
| 95 | { |
||
| 96 | $result = ''; |
||
| 97 | foreach ($this->getChunks() as $chunk) { |
||
| 98 | $result .= $chunk['data']->bin; |
||
| 99 | } |
||
| 100 | |||
| 101 | return $result; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * This method returns a stream resource that can be used to read the stored file with all file functions in PHP. |
||
| 106 | * 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. |
||
| 107 | * At most two GridFSFile chunks will be loaded in memory. |
||
| 108 | * |
||
| 109 | * @link http://php.net/manual/en/mongogridfsfile.getresource.php |
||
| 110 | * @return resource Returns a resource that can be used to read the file with |
||
| 111 | */ |
||
| 112 | public function getResource() |
||
| 113 | { |
||
| 114 | $handle = fopen('php://temp', 'w+'); |
||
| 115 | $this->copyToResource($handle); |
||
| 116 | rewind($handle); |
||
| 117 | |||
| 118 | return $handle; |
||
| 119 | } |
||
| 120 | |||
| 121 | private function copyToResource($handle) |
||
| 122 | { |
||
| 123 | $written = 0; |
||
| 124 | foreach ($this->getChunks() as $chunk) { |
||
| 125 | $written += fwrite($handle, $chunk['data']->bin); |
||
| 126 | } |
||
| 127 | |||
| 128 | return $written; |
||
| 129 | } |
||
| 130 | |||
| 131 | private function getChunks() |
||
| 139 | } |
||
| 140 |
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.