PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/Nette/Web/HttpUploadedFile.php

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