Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationLatte
      • ApplicationTracy
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsLatte
      • Framework
      • HttpTracy
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Templating
    • Utils
  • NetteModule
  • none
  • Tracy

Classes

  • BaseControl
  • Button
  • Checkbox
  • CheckboxList
  • ChoiceControl
  • CsrfProtection
  • HiddenField
  • ImageButton
  • MultiChoiceControl
  • MultiSelectBox
  • RadioList
  • SelectBox
  • SubmitButton
  • TextArea
  • TextBase
  • TextInput
  • UploadControl
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://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\FileUpload;
 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:      * @param  bool  allows to upload multiple files
 25:      */
 26:     public function __construct($label = NULL, $multiple = FALSE)
 27:     {
 28:         parent::__construct($label);
 29:         $this->control->type = 'file';
 30:         $this->control->multiple = (bool) $multiple;
 31:     }
 32: 
 33: 
 34:     /**
 35:      * This method will be called when the component (or component's parent)
 36:      * becomes attached to a monitored object. Do not call this method yourself.
 37:      * @param  Nette\ComponentModel\IComponent
 38:      * @return void
 39:      */
 40:     protected function attached($form)
 41:     {
 42:         if ($form instanceof Nette\Forms\Form) {
 43:             if ($form->getMethod() !== Nette\Forms\Form::POST) {
 44:                 throw new Nette\InvalidStateException('File upload requires method POST.');
 45:             }
 46:             $form->getElementPrototype()->enctype = 'multipart/form-data';
 47:         }
 48:         parent::attached($form);
 49:     }
 50: 
 51: 
 52:     /**
 53:      * Loads HTTP data.
 54:      * @return void
 55:      */
 56:     public function loadHttpData()
 57:     {
 58:         $this->value = $this->getHttpData(Nette\Forms\Form::DATA_FILE);
 59:         if ($this->value === NULL) {
 60:             $this->value = new FileUpload(NULL);
 61:         }
 62:     }
 63: 
 64: 
 65:     /**
 66:      * Returns HTML name of control.
 67:      * @return string
 68:      */
 69:     public function getHtmlName()
 70:     {
 71:         return parent::getHtmlName() . ($this->control->multiple ? '[]' : '');
 72:     }
 73: 
 74: 
 75:     /**
 76:      * @return self
 77:      */
 78:     public function setValue($value)
 79:     {
 80:         return $this;
 81:     }
 82: 
 83: 
 84:     /**
 85:      * Has been any file uploaded?
 86:      * @return bool
 87:      */
 88:     public function isFilled()
 89:     {
 90:         return $this->value instanceof FileUpload ? $this->value->isOk() : (bool) $this->value; // ignore NULL object
 91:     }
 92: 
 93: 
 94:     /********************* validators ****************d*g**/
 95: 
 96: 
 97:     /**
 98:      * Is file size in limit?
 99:      * @return bool
100:      */
101:     public static function validateFileSize(UploadControl $control, $limit)
102:     {
103:         foreach (static::toArray($control->getValue()) as $file) {
104:             if ($file->getSize() > $limit || $file->getError() === UPLOAD_ERR_INI_SIZE) {
105:                 return FALSE;
106:             }
107:         }
108:         return TRUE;
109:     }
110: 
111: 
112:     /**
113:      * Has file specified mime type?
114:      * @return bool
115:      */
116:     public static function validateMimeType(UploadControl $control, $mimeType)
117:     {
118:         $mimeTypes = is_array($mimeType) ? $mimeType : explode(',', $mimeType);
119:         foreach (static::toArray($control->getValue()) as $file) {
120:             $type = strtolower($file->getContentType());
121:             if (!in_array($type, $mimeTypes, TRUE) && !in_array(preg_replace('#/.*#', '/*', $type), $mimeTypes, TRUE)) {
122:                 return FALSE;
123:             }
124:         }
125:         return TRUE;
126:     }
127: 
128: 
129:     /**
130:      * Is file image?
131:      * @return bool
132:      */
133:     public static function validateImage(UploadControl $control)
134:     {
135:         foreach (static::toArray($control->getValue()) as $file) {
136:             if (!$file->isImage()) {
137:                 return FALSE;
138:             }
139:         }
140:         return TRUE;
141:     }
142: 
143: 
144:     /**
145:      * @return array
146:      */
147:     private static function toArray($value)
148:     {
149:         return $value instanceof FileUpload ? array($value) : (array) $value;
150:     }
151: 
152: }
153: 
Nette 2.2.2 API API documentation generated by ApiGen 2.8.0