temp.class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Temporary file/folder. The file/folder is automatically deleted at
  4. * the end of the script/during garbage collection.
  5. *
  6. * The object implements __toString so it can be used as string variable.
  7. *
  8. * Usage
  9. *
  10. * $path = Temp::file();
  11. * file_puts_content($path, $content);
  12. *
  13. * or
  14. *
  15. * $path = Temp::dir();
  16. * ...
  17. *
  18. * @copyright (c) 2012 University of Geneva
  19. * @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html
  20. * @author Laurent Opprecht <laurent@opprecht.info>
  21. */
  22. class Temp
  23. {
  24. /**
  25. * Recursively delete files and/or folders.
  26. *
  27. * @param string $path
  28. * @return boolean
  29. */
  30. public static function delete($path)
  31. {
  32. if (!file_exists($path)) {
  33. return false;
  34. }
  35. if (is_file($path)) {
  36. unlink($path);
  37. return true;
  38. }
  39. $files = scandir($path);
  40. $files = array_diff($files, array('.', '..'));
  41. foreach ($files as $file) {
  42. self::delete($file);
  43. }
  44. rmdir($path);
  45. }
  46. private static $temp_root = '';
  47. /**
  48. * Set the temp root directory. Temporary files are by default created in this directory.
  49. * Defaults to sys_get_temp_dir().
  50. *
  51. * @param string $value
  52. */
  53. public static function set_temp_root($value)
  54. {
  55. self::$temp_root = $value;
  56. }
  57. public static function get_temp_root()
  58. {
  59. if (empty(self::$temp_root)) {
  60. self::$temp_root = sys_get_temp_dir();
  61. }
  62. return self::$temp_root;
  63. }
  64. /**
  65. * Returns a path to a non-existing temporary file located under temp_dir.
  66. *
  67. * @return string
  68. */
  69. public static function get_temporary_name()
  70. {
  71. $result = self::get_temp_root() . '/' . md5(uniqid('tmp', true));
  72. while (file_exists($result)) {
  73. $result = self::get_temp_root() . '/' . md5(uniqid('tmp', true));
  74. }
  75. return $result;
  76. }
  77. /**
  78. *
  79. * @param string $path
  80. * @return Temp
  81. */
  82. public static function file($path = '')
  83. {
  84. $path = $path ? $path : self::get_temporary_name();
  85. return new self($path);
  86. }
  87. /**
  88. *
  89. * @param string $path
  90. * @return Temp
  91. */
  92. public static function dir($path = '')
  93. {
  94. $path = $path ? $path : self::get_temporary_name();
  95. if (!file_exists($path)) {
  96. mkdir($path, 0777, $true);
  97. }
  98. return new self($path);
  99. }
  100. /**
  101. *
  102. * @param string $path
  103. * @return Temp
  104. */
  105. public static function create($path = '')
  106. {
  107. $path = $path ? $path : self::get_temporary_name();
  108. return new self($path);
  109. }
  110. protected $path = '';
  111. function __construct($path = '')
  112. {
  113. $this->path = $path;
  114. }
  115. function get_path()
  116. {
  117. return $this->path;
  118. }
  119. function __toString()
  120. {
  121. return $this->path;
  122. }
  123. function __destruct()
  124. {
  125. $path = $this->path;
  126. self::delete($path);
  127. }
  128. }