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:         $this->monitor('Nette\Forms\Form');
  42. 42:         parent::__construct($label);
  43. 43:         $this->control->type 'file';
  44. 44:     }
  45. 45:  
  46. 46:  
  47. 47:  
  48. 48:     /**
  49. 49:      * This method will be called when the component (or component's parent)
  50. 50:      * becomes attached to a monitored object. Do not call this method yourself.
  51. 51:      * @param  IComponent 
  52. 52:      * @return void 
  53. 53:      */
  54. 54:     protected function attached($form)
  55. 55:     {
  56. 56:         if ($form instanceof Form{
  57. 57:             $form->getElementPrototype()->enctype 'multipart/form-data';
  58. 58:         }
  59. 59:     }
  60. 60:  
  61. 61:  
  62. 62:  
  63. 63:     /**
  64. 64:      * Sets control's value.
  65. 65:      * @param  array|Nette\Web\HttpUploadedFile
  66. 66:      * @return void 
  67. 67:      */
  68. 68:     public function setValue($value)
  69. 69:     {
  70. 70:         if (is_array($value)) {
  71. 71:             $this->value = new HttpUploadedFile($value);
  72. 72:  
  73. 73:         elseif ($value instanceof HttpUploadedFile{
  74. 74:             $this->value = $value;
  75. 75:  
  76. 76:         else {
  77. 77:             $this->value = new HttpUploadedFile(NULL);
  78. 78:         }
  79. 79:     }
  80. 80:  
  81. 81:  
  82. 82:  
  83. 83:     /**
  84. 84:      * Filled validator: has been any file uploaded?
  85. 85:      * @param  IFormControl 
  86. 86:      * @return bool 
  87. 87:      */
  88. 88:     public static function validateFilled(IFormControl $control)
  89. 89:     {
  90. 90:         $file $control->getValue();
  91. 91:         return $file instanceof HttpUploadedFile && $file->isOK();
  92. 92:     }
  93. 93:  
  94. 94:  
  95. 95:  
  96. 96:     /**
  97. 97:      * FileSize validator: is file size in limit?
  98. 98:      * @param  FileUpload 
  99. 99:      * @param  int  file size limit
  100. 100:      * @return bool 
  101. 101:      */
  102. 102:     public static function validateFileSize(FileUpload $control$limit)
  103. 103:     {
  104. 104:         $file $control->getValue();
  105. 105:         return $file instanceof HttpUploadedFile && $file->getSize(<= $limit;
  106. 106:     }
  107. 107:  
  108. 108:  
  109. 109:  
  110. 110:     /**
  111. 111:      * MimeType validator: has file specified mime type?
  112. 112:      * @param  FileUpload 
  113. 113:      * @param  array|string mime type
  114. 114:      * @return bool 
  115. 115:      */
  116. 116:     public static function validateMimeType(FileUpload $control$mimeType)
  117. 117:     {
  118. 118:         $file $control->getValue();
  119. 119:         if ($file instanceof HttpUploadedFile{
  120. 120:             $type $file->getContentType();
  121. 121:             $type strtolower(preg_replace('#\s*;.*$#'''$type));
  122. 122:             if (!$type{
  123. 123:                 return FALSE// cannot verify :-(
  124. 124:             }
  125. 125:             $mimeTypes is_array($mimeType$mimeType explode(','$mimeType);
  126. 126:             if (in_array($type$mimeTypesTRUE)) {
  127. 127:                 return TRUE;
  128. 128:             }
  129. 129:             if (in_array(preg_replace('#/.*#''/*'$type)$mimeTypesTRUE)) {
  130. 130:                 return TRUE;
  131. 131:             }
  132. 132:         }
  133. 133:         return FALSE;
  134. 134:     }
  135. 135: