/classes/Library/Upload.php

https://github.com/masterscript/babysmile.dp.ua · PHP · 190 lines · 74 code · 46 blank · 70 comment · 7 complexity · 6a197f48dea724bdb81861401347f5ce MD5 · raw file

  1. <?php
  2. /**
  3. * Класс для работы с загрузкой файлов на сервер
  4. *
  5. */
  6. class _Upload extends HTTP_Upload {
  7. /**
  8. * Объект для работы с загруженным файлом
  9. *
  10. * @var HTTP_Upload_File
  11. */
  12. public $objectFiles;
  13. /**
  14. * Объект конфигурационного файла
  15. *
  16. * @var Admin_Template_config
  17. */
  18. private $objectConfig;
  19. /**
  20. * Директория для загрузки файла
  21. *
  22. * @var string
  23. */
  24. protected $upload_dir;
  25. /**
  26. * @see HTTP_Upload::HTTP_Upload
  27. *
  28. */
  29. public function __construct($file_name,$lang=null) {
  30. parent::__construct($lang);
  31. $this->objectConfig = Admin_Core::getObjectGlobalConfig();
  32. $this->objectFiles = $this->getFiles($file_name);
  33. $this->objectFiles->setName('real');
  34. }
  35. /**
  36. * Устанавливает директорию для загрузки файлов в соответствии с глобальной конфигурацией
  37. *
  38. * @param string $section
  39. * @param string $name
  40. * @param string $postfix
  41. */
  42. public function setUploadDir ($section,$name,$postfix='') {
  43. $this->upload_dir = FRONT_SITE_PATH.'/'.$this->objectConfig->getConfigSection($section,$name);
  44. if ($postfix) {
  45. self::create_dirs($postfix,$this->upload_dir);
  46. $this->upload_dir = $this->upload_dir.$postfix;
  47. }
  48. }
  49. public static function create_dirs($path,$start_dir) {
  50. $path = str_replace(array('\\\\','\\'),'/',$path);
  51. $parts = explode('/',$path);
  52. $cumulate_path = '';
  53. foreach ($parts as $part) {
  54. if (!$part)
  55. continue;
  56. $cumulate_path = $cumulate_path.'/'.$part;
  57. if (!file_exists($start_dir.'/'.$cumulate_path)) {
  58. // try to create
  59. if (!mkdir($start_dir.'/'.$cumulate_path,0775)) {
  60. throw new Admin_FileException('Невозможно создать каталог: '.$start_dir.'/'.$cumulate_path);
  61. }
  62. }
  63. }
  64. }
  65. /**
  66. * Возвращает директорию для загрузки файлов
  67. *
  68. * @return string
  69. */
  70. public function getUploadDir () {
  71. return $this->upload_dir;
  72. }
  73. /**
  74. * Возвращает имя временного файла
  75. *
  76. * @return string
  77. */
  78. public function getTmpName () {
  79. return $this->objectFiles->getProp('tmp_name');
  80. }
  81. /**
  82. * Выполняет загрузку файла
  83. */
  84. public function upload () {
  85. $this->objectFiles->_chmod = 0777;
  86. $this->objectFiles->moveTo($this->upload_dir);
  87. if ($this->objectFiles->isError()) {
  88. throw new Admin_FileException($this->objectFiles->getMessage());
  89. }
  90. }
  91. /**
  92. * Возвращает параметр секции конфигурационного файла
  93. *
  94. * @param string $section
  95. * @param string $name
  96. * @return mixed
  97. */
  98. public function getConfigParam ($section,$name) {
  99. return $this->objectConfig->getConfigSection($section,$name);
  100. }
  101. /**
  102. * Проверяет соответствие размеров изображения
  103. *
  104. * @param integer $need_width
  105. * @param integer $need_height
  106. * @return bool
  107. */
  108. public function checkImageSize ($need_width,$need_height) {
  109. list($width,$height) = getimagesize($this->objectFiles->getProp('tmp_name'));
  110. return ($width==$need_width && $height==$need_height);
  111. }
  112. public function getImageWidth() {
  113. $size = getimagesize($this->objectFiles->getProp('tmp_name'));
  114. return $size[0];
  115. }
  116. public function checkMaxImageSize ($max_width,$max_height) {
  117. list($width,$height) = getimagesize($this->objectFiles->getProp('tmp_name'));
  118. return ($width<=$max_width && $height<=$max_height);
  119. }
  120. public function getImageHeight() {
  121. $size = getimagesize($this->objectFiles->getProp('tmp_name'));
  122. return $size[1];
  123. }
  124. /**
  125. * Проверяет соответствие размеру
  126. *
  127. * @param integer $size
  128. * @return bool
  129. */
  130. public function checkSize ($size) {
  131. return $this->objectFiles->getProp('size')<=$size;
  132. }
  133. /**
  134. * Проверяет соответствие MIME типу
  135. *
  136. * @param string $allowed_types
  137. * @return bool
  138. */
  139. public function checkType ($allowed_types) {
  140. $allowed_types = Admin_Template_Config::explode(',',$allowed_types);
  141. return in_array($this->objectFiles->getProp('type'),$allowed_types);
  142. }
  143. }
  144. ?>