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