PageRenderTime 57ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/Nette/Forms/Controls/FileUpload.php

https://github.com/DocX/nette
PHP | 144 lines | 60 code | 28 blank | 56 comment | 10 complexity | 5d82b6b3c0a0d3dbaae4d0ead2fb564b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Nette Framework
  4. *
  5. * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
  6. *
  7. * This source file is subject to the "Nette license" that is bundled
  8. * with this package in the file license.txt.
  9. *
  10. * For more information please see http://nettephp.com
  11. *
  12. * @copyright Copyright (c) 2004, 2009 David Grudl
  13. * @license http://nettephp.com/license Nette license
  14. * @link http://nettephp.com
  15. * @category Nette
  16. * @package Nette\Forms
  17. */
  18. /*namespace Nette\Forms;*/
  19. require_once dirname(__FILE__) . '/../../Forms/Controls/FormControl.php';
  20. /*use Nette\Web\HttpUploadedFile;*/
  21. /**
  22. * Text box and browse button that allow users to select a file to upload to the server.
  23. *
  24. * @author David Grudl
  25. * @copyright Copyright (c) 2004, 2009 David Grudl
  26. * @package Nette\Forms
  27. */
  28. class FileUpload extends FormControl
  29. {
  30. /**
  31. * @param string label
  32. */
  33. public function __construct($label = NULL)
  34. {
  35. parent::__construct($label);
  36. $this->control->type = 'file';
  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. * Sets control's value.
  56. * @param array|Nette\Web\HttpUploadedFile
  57. * @return FileUpload provides a fluent interface
  58. */
  59. public function setValue($value)
  60. {
  61. if (is_array($value)) {
  62. $this->value = new HttpUploadedFile($value);
  63. } elseif ($value instanceof HttpUploadedFile) {
  64. $this->value = $value;
  65. } else {
  66. $this->value = new HttpUploadedFile(NULL);
  67. }
  68. return $this;
  69. }
  70. /**
  71. * Filled validator: has been any file uploaded?
  72. * @param IFormControl
  73. * @return bool
  74. */
  75. public static function validateFilled(IFormControl $control)
  76. {
  77. $file = $control->getValue();
  78. return $file instanceof HttpUploadedFile && $file->isOK();
  79. }
  80. /**
  81. * FileSize validator: is file size in limit?
  82. * @param FileUpload
  83. * @param int file size limit
  84. * @return bool
  85. */
  86. public static function validateFileSize(FileUpload $control, $limit)
  87. {
  88. $file = $control->getValue();
  89. return $file instanceof HttpUploadedFile && $file->getSize() <= $limit;
  90. }
  91. /**
  92. * MimeType validator: has file specified mime type?
  93. * @param FileUpload
  94. * @param array|string mime type
  95. * @return bool
  96. */
  97. public static function validateMimeType(FileUpload $control, $mimeType)
  98. {
  99. $file = $control->getValue();
  100. if ($file instanceof HttpUploadedFile) {
  101. $type = $file->getContentType();
  102. $type = strtolower(preg_replace('#\s*;.*$#', '', $type));
  103. if (!$type) {
  104. return FALSE; // cannot verify :-(
  105. }
  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. }