Packages

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • NButton
  • NCheckbox
  • NConventionalRenderer
  • NFileUpload
  • NForm
  • NFormContainer
  • NFormGroup
  • NHiddenField
  • NImageButton
  • NInstantClientScript
  • NMultiSelectBox
  • NRadioList
  • NRule
  • NRules
  • NSelectBox
  • NSubmitButton
  • NTextArea
  • NTextBase
  • NTextInput

Interfaces

  • IFormControl
  • IFormRenderer
  • INamingContainer
  • ISubmitterControl
  • Overview
  • Package
  • Class
  • Tree
  • Other releases
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://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
 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
 20:  */
 21: class NFileUpload 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:     /**
 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 NForm) {
 44:             if ($form->getMethod() !== NForm::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|NHttpUploadedFile
 57:      * @return NFileUpload  provides a fluent interface
 58:      */
 59:     public function setValue($value)
 60:     {
 61:         if (is_array($value)) {
 62:             $this->value = new NHttpUploadedFile($value);
 63: 
 64:         } elseif ($value instanceof NHttpUploadedFile) {
 65:             $this->value = $value;
 66: 
 67:         } else {
 68:             $this->value = new NHttpUploadedFile(NULL);
 69:         }
 70:         return $this;
 71:     }
 72: 
 73: 
 74: 
 75:     /**
 76:      * Filled validator: has been any file uploaded?
 77:      * @param  IFormControl
 78:      * @return bool
 79:      */
 80:     public static function validateFilled(IFormControl $control)
 81:     {
 82:         $file = $control->getValue();
 83:         return $file instanceof NHttpUploadedFile && $file->isOK();
 84:     }
 85: 
 86: 
 87: 
 88:     /**
 89:      * FileSize validator: is file size in limit?
 90:      * @param  NFileUpload
 91:      * @param  int  file size limit
 92:      * @return bool
 93:      */
 94:     public static function validateFileSize(NFileUpload $control, $limit)
 95:     {
 96:         $file = $control->getValue();
 97:         return $file instanceof NHttpUploadedFile && $file->getSize() <= $limit;
 98:     }
 99: 
100: 
101: 
102:     /**
103:      * MimeType validator: has file specified mime type?
104:      * @param  NFileUpload
105:      * @param  array|string  mime type
106:      * @return bool
107:      */
108:     public static function validateMimeType(NFileUpload $control, $mimeType)
109:     {
110:         $file = $control->getValue();
111:         if ($file instanceof NHttpUploadedFile) {
112:             $type = strtolower($file->getContentType());
113:             $mimeTypes = is_array($mimeType) ? $mimeType : explode(',', $mimeType);
114:             if (in_array($type, $mimeTypes, TRUE)) {
115:                 return TRUE;
116:             }
117:             if (in_array(preg_replace('#/.*#', '/*', $type), $mimeTypes, TRUE)) {
118:                 return TRUE;
119:             }
120:         }
121:         return FALSE;
122:     }
123: 
124: }
125: 
Nette Framework 0.9.7 (for PHP 5.2) API documentation generated by ApiGen 2.3.0