Source for file FileUpload.php

Documentation is available at FileUpload.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\Forms
  18. 18:  */
  19. 19:  
  20. 20:  
  21. 21:  
  22. 22: require_once dirname(__FILE__'/../../Forms/Controls/FormControl.php';
  23. 23:  
  24. 24:  
  25. 25:  
  26. 26: /**
  27. 27:  * Text box and browse button that allow users to select a file to upload to the server.
  28. 28:  *
  29. 29:  * @author     David Grudl
  30. 30:  * @copyright  Copyright (c) 2004, 2009 David Grudl
  31. 31:  * @package    Nette\Forms
  32. 32:  */
  33. 33: class FileUpload extends FormControl
  34. 34: {
  35. 35:  
  36. 36:     /**
  37. 37:      * @param  string  label
  38. 38:      */
  39. 39:     public function __construct($label NULL)
  40. 40:     {
  41. 41:         parent::__construct($label);
  42. 42:         $this->control->type 'file';
  43. 43:     }
  44. 44:  
  45. 45:  
  46. 46:  
  47. 47:     /**
  48. 48:      * This method will be called when the component (or component's parent)
  49. 49:      * becomes attached to a monitored object. Do not call this method yourself.
  50. 50:      * @param  IComponent 
  51. 51:      * @return void 
  52. 52:      */
  53. 53:     protected function attached($form)
  54. 54:     {
  55. 55:         if ($form instanceof Form{
  56. 56:             if ($form->getMethod(!== Form::POST{
  57. 57:                 throw new InvalidStateException('File upload requires method POST.');
  58. 58:             }
  59. 59:             $form->getElementPrototype()->enctype 'multipart/form-data';
  60. 60:         }
  61. 61:         parent::attached($form);
  62. 62:     }
  63. 63:  
  64. 64:  
  65. 65:  
  66. 66:     /**
  67. 67:      * Sets control's value.
  68. 68:      * @param  array|Nette\Web\HttpUploadedFile
  69. 69:      * @return FileUpload  provides a fluent interface
  70. 70:      */
  71. 71:     public function setValue($value)
  72. 72:     {
  73. 73:         if (is_array($value)) {
  74. 74:             $this->value = new HttpUploadedFile($value);
  75. 75:  
  76. 76:         elseif ($value instanceof HttpUploadedFile{
  77. 77:             $this->value = $value;
  78. 78:  
  79. 79:         else {
  80. 80:             $this->value = new HttpUploadedFile(NULL);
  81. 81:         }
  82. 82:         return $this;
  83. 83:     }
  84. 84:  
  85. 85:  
  86. 86:  
  87. 87:     /**
  88. 88:      * Filled validator: has been any file uploaded?
  89. 89:      * @param  IFormControl 
  90. 90:      * @return bool 
  91. 91:      */
  92. 92:     public static function validateFilled(IFormControl $control)
  93. 93:     {
  94. 94:         $file $control->getValue();
  95. 95:         return $file instanceof HttpUploadedFile && $file->isOK();
  96. 96:     }
  97. 97:  
  98. 98:  
  99. 99:  
  100. 100:     /**
  101. 101:      * FileSize validator: is file size in limit?
  102. 102:      * @param  FileUpload 
  103. 103:      * @param  int  file size limit
  104. 104:      * @return bool 
  105. 105:      */
  106. 106:     public static function validateFileSize(FileUpload $control$limit)
  107. 107:     {
  108. 108:         $file $control->getValue();
  109. 109:         return $file instanceof HttpUploadedFile && $file->getSize(<= $limit;
  110. 110:     }
  111. 111:  
  112. 112:  
  113. 113:  
  114. 114:     /**
  115. 115:      * MimeType validator: has file specified mime type?
  116. 116:      * @param  FileUpload 
  117. 117:      * @param  array|string mime type
  118. 118:      * @return bool 
  119. 119:      */
  120. 120:     public static function validateMimeType(FileUpload $control$mimeType)
  121. 121:     {
  122. 122:         $file $control->getValue();
  123. 123:         if ($file instanceof HttpUploadedFile{
  124. 124:             $type $file->getContentType();
  125. 125:             $type strtolower(preg_replace('#\s*;.*$#'''$type));
  126. 126:             if (!$type{
  127. 127:                 return FALSE// cannot verify :-(
  128. 128:             }
  129. 129:             $mimeTypes is_array($mimeType$mimeType explode(','$mimeType);
  130. 130:             if (in_array($type$mimeTypesTRUE)) {
  131. 131:                 return TRUE;
  132. 132:             }
  133. 133:             if (in_array(preg_replace('#/.*#''/*'$type)$mimeTypesTRUE)) {
  134. 134:                 return TRUE;
  135. 135:             }
  136. 136:         }
  137. 137:         return FALSE;
  138. 138:     }
  139. 139: