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