PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/Nette/Web/HttpUploadedFile.php

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