Namespaces

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

Classes

  • Button
  • Checkbox
  • ConventionalRenderer
  • FileUpload
  • Form
  • FormContainer
  • FormControl
  • FormGroup
  • HiddenField
  • ImageButton
  • InstantClientScript
  • MultiSelectBox
  • RadioList
  • Rule
  • Rules
  • SelectBox
  • SubmitButton
  • TextArea
  • TextBase
  • TextInput

Interfaces

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