MongoGridFSFile.php 4.3 KB

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