Source for file HttpUploadedFile.php

Documentation is available at HttpUploadedFile.php

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