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
  • PHP

Classes

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