Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • none

Classes

  • BaseControl
  • Button
  • Checkbox
  • HiddenField
  • ImageButton
  • MultiSelectBox
  • RadioList
  • SelectBox
  • SubmitButton
  • TextArea
  • TextBase
  • TextInput
  • UploadControl
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  • Nette homepage
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Forms\Controls;
  9: 
 10: use Nette,
 11:     Nette\Http;
 12: 
 13: 
 14: /**
 15:  * Text box and browse button that allow users to select a file to upload to the server.
 16:  *
 17:  * @author     David Grudl
 18:  */
 19: class UploadControl extends BaseControl
 20: {
 21: 
 22:     /**
 23:      * @param  string  label
 24:      */
 25:     public function __construct($label = NULL)
 26:     {
 27:         parent::__construct($label);
 28:         $this->control->type = 'file';
 29:     }
 30: 
 31: 
 32:     /**
 33:      * This method will be called when the component (or component's parent)
 34:      * becomes attached to a monitored object. Do not call this method yourself.
 35:      * @param  Nette\ComponentModel\IComponent
 36:      * @return void
 37:      */
 38:     protected function attached($form)
 39:     {
 40:         if ($form instanceof Nette\Forms\Form) {
 41:             if ($form->getMethod() !== Nette\Forms\Form::POST) {
 42:                 throw new Nette\InvalidStateException('File upload requires method POST.');
 43:             }
 44:             $form->getElementPrototype()->enctype = 'multipart/form-data';
 45:         }
 46:         parent::attached($form);
 47:     }
 48: 
 49: 
 50:     /**
 51:      * Sets control's value.
 52:      * @param  array|Nette\Http\FileUpload
 53:      * @return self
 54:      */
 55:     public function setValue($value)
 56:     {
 57:         if (is_array($value)) {
 58:             $this->value = new Http\FileUpload($value);
 59: 
 60:         } elseif ($value instanceof Http\FileUpload) {
 61:             $this->value = $value;
 62: 
 63:         } else {
 64:             $this->value = new Http\FileUpload(NULL);
 65:         }
 66:         return $this;
 67:     }
 68: 
 69: 
 70:     /**
 71:      * Has been any file uploaded?
 72:      * @return bool
 73:      */
 74:     public function isFilled()
 75:     {
 76:         return $this->value instanceof Http\FileUpload && $this->value->isOK();
 77:     }
 78: 
 79: 
 80:     /********************* validators ****************d*g**/
 81: 
 82: 
 83:     /**
 84:      * Is file size in limit?
 85:      * @return bool
 86:      * @internal
 87:      */
 88:     public static function validateFileSize(UploadControl $control, $limit)
 89:     {
 90:         $file = $control->getValue();
 91:         return $file instanceof Http\FileUpload && $file->getSize() <= $limit;
 92:     }
 93: 
 94: 
 95:     /**
 96:      * Has file specified mime type?
 97:      * @return bool
 98:      * @internal
 99:      */
100:     public static function validateMimeType(UploadControl $control, $mimeType)
101:     {
102:         $file = $control->getValue();
103:         if ($file instanceof Http\FileUpload) {
104:             $type = strtolower($file->getContentType());
105:             $mimeTypes = is_array($mimeType) ? $mimeType : explode(',', $mimeType);
106:             if (in_array($type, $mimeTypes, TRUE)) {
107:                 return TRUE;
108:             }
109:             if (in_array(preg_replace('#/.*#', '/*', $type), $mimeTypes, TRUE)) {
110:                 return TRUE;
111:             }
112:         }
113:         return FALSE;
114:     }
115: 
116: 
117:     /**
118:      * Is file image?
119:      * @return bool
120:      * @internal
121:      */
122:     public static function validateImage(UploadControl $control)
123:     {
124:         $file = $control->getValue();
125:         return $file instanceof Http\FileUpload && $file->isImage();
126:     }
127: 
128: }
129: 
Nette 2.0 API documentation generated by ApiGen 2.8.0