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

  • Button
  • Checkbox
  • FormControl
  • HiddenField
  • ImageButton
  • MultiSelectBox
  • RadioList
  • SelectBox
  • SubmitButton
  • TextArea
  • TextBase
  • TextInput
  • UploadControl
  • Overview
  • Package
  • 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:  * @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 UploadControl extends FormControl
 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:     /**
 36:      * This method will be called when the component (or component's parent)
 37:      * becomes attached to a monitored object. Do not call this method yourself.
 38:      * @param  IComponent
 39:      * @return void
 40:      */
 41:     protected function attached($form)
 42:     {
 43:         if ($form instanceof Form) {
 44:             if ($form->getMethod() !== Form::POST) {
 45:                 throw new InvalidStateException('File upload requires method POST.');
 46:             }
 47:             $form->getElementPrototype()->enctype = 'multipart/form-data';
 48:         }
 49:         parent::attached($form);
 50:     }
 51: 
 52: 
 53: 
 54:     /**
 55:      * Sets control's value.
 56:      * @param  array|HttpUploadedFile
 57:      * @return HttpUploadedFile  provides a fluent interface
 58:      */
 59:     public function setValue($value)
 60:     {
 61:         if (is_array($value)) {
 62:             $this->value = new HttpUploadedFile($value);
 63: 
 64:         } elseif ($value instanceof HttpUploadedFile) {
 65:             $this->value = $value;
 66: 
 67:         } else {
 68:             $this->value = new HttpUploadedFile(NULL);
 69:         }
 70:         return $this;
 71:     }
 72: 
 73: 
 74: 
 75:     /**
 76:      * Has been any file uploaded?
 77:      * @return bool
 78:      */
 79:     public function isFilled()
 80:     {
 81:         return $this->value instanceof HttpUploadedFile && $this->value->isOK();
 82:     }
 83: 
 84: 
 85: 
 86:     /**
 87:      * FileSize validator: is file size in limit?
 88:      * @param  UploadControl
 89:      * @param  int  file size limit
 90:      * @return bool
 91:      */
 92:     public static function validateFileSize(UploadControl $control, $limit)
 93:     {
 94:         $file = $control->getValue();
 95:         return $file instanceof HttpUploadedFile && $file->getSize() <= $limit;
 96:     }
 97: 
 98: 
 99: 
100:     /**
101:      * MimeType validator: has file specified mime type?
102:      * @param  UploadControl
103:      * @param  array|string  mime type
104:      * @return bool
105:      */
106:     public static function validateMimeType(UploadControl $control, $mimeType)
107:     {
108:         $file = $control->getValue();
109:         if ($file instanceof HttpUploadedFile) {
110:             $type = strtolower($file->getContentType());
111:             $mimeTypes = is_array($mimeType) ? $mimeType : explode(',', $mimeType);
112:             if (in_array($type, $mimeTypes, TRUE)) {
113:                 return TRUE;
114:             }
115:             if (in_array(preg_replace('#/.*#', '/*', $type), $mimeTypes, TRUE)) {
116:                 return TRUE;
117:             }
118:         }
119:         return FALSE;
120:     }
121: 
122: 
123: 
124:     /**
125:      * Image validator: is file image?
126:      * @param  UploadControl
127:      * @return bool
128:      */
129:     public static function validateImage(UploadControl $control)
130:     {
131:         $file = $control->getValue();
132:         return $file instanceof HttpUploadedFile && $file->isImage();
133:     }
134: 
135: }
136: 
Nette Framework 2.0.0 (for PHP 5.2, un-prefixed) API API documentation generated by ApiGen 2.7.0