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