MongoGridFSFile.php 4.3 KB

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