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