PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/www/libs/nette-dev/Web/HttpUploadedFile.php

https://github.com/bazo/Mokuji
PHP | 211 lines | 89 code | 48 blank | 74 comment | 8 complexity | d70caa57d8ad5c96e320ad05b2d4c290 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * @copyright Copyright (c) 2004, 2010 David Grudl
  6. * @license http://nettephp.com/license Nette license
  7. * @link http://nettephp.com
  8. * @category Nette
  9. * @package Nette\Web
  10. */
  11. /**
  12. * Provides access to individual files that have been uploaded by a client.
  13. *
  14. * @copyright Copyright (c) 2004, 2010 David Grudl
  15. * @package Nette\Web
  16. *
  17. * @property-read string $name
  18. * @property-read string $contentType
  19. * @property-read int $size
  20. * @property-read string $temporaryFile
  21. * @property-read Image $image
  22. * @property-read int $error
  23. * @property-read array $imageSize
  24. * @property-read bool $ok
  25. */
  26. class HttpUploadedFile extends Object
  27. {
  28. /* @var string */
  29. private $name;
  30. /* @var string */
  31. private $type;
  32. /* @var string */
  33. private $size;
  34. /* @var string */
  35. private $tmpName;
  36. /* @var int */
  37. private $error;
  38. public function __construct($value)
  39. {
  40. foreach (array('name', 'type', 'size', 'tmp_name', 'error') as $key) {
  41. if (!isset($value[$key]) || !is_scalar($value[$key])) {
  42. $this->error = UPLOAD_ERR_NO_FILE;
  43. return; // or throw exception?
  44. }
  45. }
  46. $this->name = $value['name'];
  47. $this->size = $value['size'];
  48. $this->tmpName = $value['tmp_name'];
  49. $this->error = $value['error'];
  50. }
  51. /**
  52. * Returns the file name.
  53. * @return string
  54. */
  55. public function getName()
  56. {
  57. return $this->name;
  58. }
  59. /**
  60. * Returns the MIME content type of an uploaded file.
  61. * @return string
  62. */
  63. public function getContentType()
  64. {
  65. if ($this->isOk() && $this->type === NULL) {
  66. $info = getimagesize($this->tmpName);
  67. if (isset($info['mime'])) {
  68. $this->type = $info['mime'];
  69. } elseif (extension_loaded('fileinfo')) {
  70. $this->type = finfo_file(finfo_open(FILEINFO_MIME), $this->tmpName);
  71. } elseif (function_exists('mime_content_type')) {
  72. $this->type = mime_content_type($this->tmpName);
  73. }
  74. if (!$this->type) {
  75. $this->type = 'application/octet-stream';
  76. }
  77. }
  78. return $this->type;
  79. }
  80. /**
  81. * Returns the size of an uploaded file.
  82. * @return int
  83. */
  84. public function getSize()
  85. {
  86. return $this->size;
  87. }
  88. /**
  89. * Returns the path to an uploaded file.
  90. * @return string
  91. */
  92. public function getTemporaryFile()
  93. {
  94. return $this->tmpName;
  95. }
  96. /**
  97. * Returns the path to an uploaded file.
  98. * @return string
  99. */
  100. public function __toString()
  101. {
  102. return $this->tmpName;
  103. }
  104. /**
  105. * Returns the error code. {@link http://php.net/manual/en/features.file-upload.errors.php}
  106. * @return int
  107. */
  108. public function getError()
  109. {
  110. return $this->error;
  111. }
  112. /**
  113. * Is there any error?
  114. * @return bool
  115. */
  116. public function isOk()
  117. {
  118. return $this->error === UPLOAD_ERR_OK;
  119. }
  120. /**
  121. * Move uploaded file to new location.
  122. * @param string
  123. * @return HttpUploadedFile provides a fluent interface
  124. */
  125. public function move($dest)
  126. {
  127. $dir = dirname($dest);
  128. if (@mkdir($dir, 0755, TRUE)) { // intentionally @
  129. chmod($dir, 0755);
  130. }
  131. $func = is_uploaded_file($this->tmpName) ? 'move_uploaded_file' : 'rename';
  132. if (!$func($this->tmpName, $dest)) {
  133. throw new InvalidStateException("Unable to move uploaded file '$this->tmpName' to '$dest'.");
  134. }
  135. chmod($dest, 0644);
  136. $this->tmpName = $dest;
  137. return $this;
  138. }
  139. /**
  140. * Is uploaded file GIF, PNG or JPEG?
  141. * @return bool
  142. */
  143. public function isImage()
  144. {
  145. return in_array($this->getContentType(), array('image/gif', 'image/png', 'image/jpeg'), TRUE);
  146. }
  147. /**
  148. * Returns the image.
  149. * @return Image
  150. */
  151. public function getImage()
  152. {
  153. return Image::fromFile($this->tmpName);
  154. }
  155. /**
  156. * Returns the dimensions of an uploaded image as array.
  157. * @return array
  158. */
  159. public function getImageSize()
  160. {
  161. return $this->isOk() ? getimagesize($this->tmpName) : NULL;
  162. }
  163. }