Packages

  • 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

  • NButton
  • NCheckbox
  • NFormControl
  • NHiddenField
  • NImageButton
  • NMultiSelectBox
  • NRadioList
  • NSelectBox
  • NSubmitButton
  • NTextArea
  • NTextBase
  • NTextInput
  • NUploadControl
  • Overview
  • Package
  • 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:  * @package Nette\Forms\Controls
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * Text box and browse button that allow users to select a file to upload to the server.
 17:  *
 18:  * @author     David Grudl
 19:  * @package Nette\Forms\Controls
 20:  */
 21: class NUploadControl extends NFormControl
 22: {
 23: 
 24:     /**
 25:      * @param  string  label
 26:      */
 27:     public function __construct($label = NULL)
 28:     {
 29:         parent::__construct($label);
 30:         $this->control->type = 'file';
 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  IComponent
 38:      * @return void
 39:      */
 40:     protected function attached($form)
 41:     {
 42:         if ($form instanceof NForm) {
 43:             if ($form->getMethod() !== NForm::POST) {
 44:                 throw new InvalidStateException('File upload requires method POST.');
 45:             }
 46:             $form->getElementPrototype()->enctype = 'multipart/form-data';
 47:         }
 48:         parent::attached($form);
 49:     }
 50: 
 51: 
 52:     /**
 53:      * Sets control's value.
 54:      * @param  array|NHttpUploadedFile
 55:      * @return self
 56:      */
 57:     public function setValue($value)
 58:     {
 59:         if (is_array($value)) {
 60:             $this->value = new NHttpUploadedFile($value);
 61: 
 62:         } elseif ($value instanceof NHttpUploadedFile) {
 63:             $this->value = $value;
 64: 
 65:         } else {
 66:             $this->value = new NHttpUploadedFile(NULL);
 67:         }
 68:         return $this;
 69:     }
 70: 
 71: 
 72:     /**
 73:      * Has been any file uploaded?
 74:      * @return bool
 75:      */
 76:     public function isFilled()
 77:     {
 78:         return $this->value instanceof NHttpUploadedFile && $this->value->isOK();
 79:     }
 80: 
 81: 
 82:     /**
 83:      * FileSize validator: is file size in limit?
 84:      * @param  NUploadControl
 85:      * @param  int  file size limit
 86:      * @return bool
 87:      */
 88:     public static function validateFileSize(NUploadControl $control, $limit)
 89:     {
 90:         $file = $control->getValue();
 91:         return $file instanceof NHttpUploadedFile && $file->getSize() <= $limit;
 92:     }
 93: 
 94: 
 95:     /**
 96:      * MimeType validator: has file specified mime type?
 97:      * @param  NUploadControl
 98:      * @param  array|string  mime type
 99:      * @return bool
100:      */
101:     public static function validateMimeType(NUploadControl $control, $mimeType)
102:     {
103:         $file = $control->getValue();
104:         if ($file instanceof NHttpUploadedFile) {
105:             $type = strtolower($file->getContentType());
106:             $mimeTypes = is_array($mimeType) ? $mimeType : explode(',', $mimeType);
107:             if (in_array($type, $mimeTypes, TRUE)) {
108:                 return TRUE;
109:             }
110:             if (in_array(preg_replace('#/.*#', '/*', $type), $mimeTypes, TRUE)) {
111:                 return TRUE;
112:             }
113:         }
114:         return FALSE;
115:     }
116: 
117: 
118:     /**
119:      * Image validator: is file image?
120:      * @return bool
121:      */
122:     public static function validateImage(NUploadControl $control)
123:     {
124:         $file = $control->getValue();
125:         return $file instanceof NHttpUploadedFile && $file->isImage();
126:     }
127: 
128: }
129: 
Nette Framework 2.0.11 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.8.0