temp.class.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. protected static $files = array();
  25. /**
  26. * Returns the list of temporary files opened by the script.
  27. * This is mostly due to pin temporary files and prevent garbage collection.
  28. * This ensure files are not unlinked while still using it to send data in
  29. * an upload.
  30. *
  31. * @return array
  32. */
  33. public static function files()
  34. {
  35. return self::$files;
  36. }
  37. /**
  38. * Recursively delete files and/or folders.
  39. *
  40. * @param string $path
  41. * @return boolean
  42. */
  43. public static function delete($path)
  44. {
  45. if (!file_exists($path)) {
  46. return false;
  47. }
  48. if (is_readable($path)) {
  49. unlink($path);
  50. return true;
  51. }
  52. if (is_dir($path)) {
  53. $files = scandir($path);
  54. $files = array_diff($files, array('.', '..'));
  55. foreach ($files as $file) {
  56. self::delete($file);
  57. }
  58. rmdir($path);
  59. }
  60. }
  61. private static $temp_root = '';
  62. /**
  63. * Set the temp root directory. Temporary files are by default created in this directory.
  64. * Defaults to sys_get_temp_dir().
  65. *
  66. * @param string $value
  67. */
  68. public static function set_temp_root($value)
  69. {
  70. self::$temp_root = $value;
  71. }
  72. public static function get_temp_root()
  73. {
  74. if (empty(self::$temp_root)) {
  75. self::$temp_root = sys_get_temp_dir();
  76. }
  77. return self::$temp_root;
  78. }
  79. /**
  80. * Returns a path to a non-existing temporary file located under temp_dir.
  81. *
  82. * @return string
  83. */
  84. public static function get_temporary_name()
  85. {
  86. $result = self::get_temp_root() . '/' . md5(uniqid('tmp', true));
  87. while (file_exists($result)) {
  88. $result = self::get_temp_root() . '/' . md5(uniqid('tmp', true));
  89. }
  90. return $result;
  91. }
  92. /**
  93. *
  94. * @param string $path
  95. * @return Temp
  96. */
  97. public static function file($path = '')
  98. {
  99. $path = $path ? $path : self::get_temporary_name();
  100. return new self($path);
  101. }
  102. /**
  103. *
  104. * @param string $path
  105. * @return Temp
  106. */
  107. public static function dir($path = '')
  108. {
  109. $path = $path ? $path : self::get_temporary_name();
  110. if (!file_exists($path)) {
  111. mkdir($path, 0777, $true);
  112. }
  113. return new self($path);
  114. }
  115. /**
  116. *
  117. * @param string $path
  118. * @return Temp
  119. */
  120. public static function create($path = '')
  121. {
  122. $path = $path ? $path : self::get_temporary_name();
  123. return new self($path);
  124. }
  125. protected $path = '';
  126. function __construct($path = '')
  127. {
  128. self::$files[] = $this;
  129. $this->path = $path;
  130. }
  131. function get_path()
  132. {
  133. return $this->path;
  134. }
  135. function __toString()
  136. {
  137. return $this->path;
  138. }
  139. function __destruct()
  140. {
  141. $path = $this->path;
  142. self::delete($path);
  143. }
  144. }