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