MongoGridFSFile.php 4.3 KB

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