TemporaryFileByteStream.php 1004 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. <?php
  2. /*
  3. * This file is part of SwiftMailer.
  4. * (c) 2004-2009 Chris Corbyn
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * @author Romain-Geissler
  11. */
  12. class Swift_ByteStream_TemporaryFileByteStream extends Swift_ByteStream_FileByteStream
  13. {
  14. public function __construct()
  15. {
  16. $filePath = tempnam(sys_get_temp_dir(), 'FileByteStream');
  17. if ($filePath === false) {
  18. throw new Swift_IoException('Failed to retrieve temporary file name.');
  19. }
  20. parent::__construct($filePath, true);
  21. }
  22. public function getContent()
  23. {
  24. if (($content = file_get_contents($this->getPath())) === false) {
  25. throw new Swift_IoException('Failed to get temporary file content.');
  26. }
  27. return $content;
  28. }
  29. public function __destruct()
  30. {
  31. if (file_exists($this->getPath())) {
  32. @unlink($this->getPath());
  33. }
  34. }
  35. }