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