2
0

File.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Form
  17. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /** Zend_Form_Element_Xhtml */
  21. require_once 'Zend/Form/Element/Xhtml.php';
  22. /**
  23. * Zend_Form_Element
  24. *
  25. * @category Zend
  26. * @package Zend_Form
  27. * @subpackage Element
  28. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. * @version $Id$
  31. */
  32. class Zend_Form_Element_File extends Zend_Form_Element_Xhtml
  33. {
  34. /**
  35. * Plugin loader type
  36. */
  37. const TRANSFER_ADAPTER = 'TRANSFER_ADAPTER';
  38. /**
  39. * @var string Default view helper
  40. */
  41. public $helper = 'formFile';
  42. /**
  43. * @var Zend_File_Transfer_Adapter_Abstract
  44. */
  45. protected $_adapter;
  46. /**
  47. * @var boolean Already validated ?
  48. */
  49. protected $_validated = false;
  50. /**
  51. * @var boolean Disable value to be equal to file content
  52. */
  53. protected $_valueDisabled = false;
  54. /**
  55. * @var integer Internal multifile counter
  56. */
  57. protected $_counter = 1;
  58. /**
  59. * @var integer Maximum file size for MAX_FILE_SIZE attribut of form
  60. */
  61. protected static $_maxFileSize = -1;
  62. /**
  63. * Load default decorators
  64. *
  65. * @return Zend_Form_Element_File
  66. */
  67. public function loadDefaultDecorators()
  68. {
  69. if ($this->loadDefaultDecoratorsIsDisabled()) {
  70. return $this;
  71. }
  72. parent::loadDefaultDecorators();
  73. // This element needs the File decorator and not the ViewHelper decorator
  74. if (false !== $this->getDecorator('ViewHelper')) {
  75. $this->removeDecorator('ViewHelper');
  76. }
  77. if (false === $this->getDecorator('File')) {
  78. // Add File decorator to the beginning
  79. $decorators = $this->getDecorators();
  80. array_unshift($decorators, 'File');
  81. $this->setDecorators($decorators);
  82. }
  83. return $this;
  84. }
  85. /**
  86. * Set plugin loader
  87. *
  88. * @param Zend_Loader_PluginLoader_Interface $loader
  89. * @param string $type
  90. * @return Zend_Form_Element_File
  91. */
  92. public function setPluginLoader(Zend_Loader_PluginLoader_Interface $loader, $type)
  93. {
  94. $type = strtoupper($type);
  95. if ($type != self::TRANSFER_ADAPTER) {
  96. return parent::setPluginLoader($loader, $type);
  97. }
  98. $this->_loaders[$type] = $loader;
  99. return $this;
  100. }
  101. /**
  102. * Get Plugin Loader
  103. *
  104. * @param string $type
  105. * @return Zend_Loader_PluginLoader_Interface
  106. */
  107. public function getPluginLoader($type)
  108. {
  109. $type = strtoupper($type);
  110. if ($type != self::TRANSFER_ADAPTER) {
  111. return parent::getPluginLoader($type);
  112. }
  113. if (!array_key_exists($type, $this->_loaders)) {
  114. require_once 'Zend/Loader/PluginLoader.php';
  115. $loader = new Zend_Loader_PluginLoader(array(
  116. 'Zend_File_Transfer_Adapter' => 'Zend/File/Transfer/Adapter/',
  117. ));
  118. $this->setPluginLoader($loader, self::TRANSFER_ADAPTER);
  119. }
  120. return $this->_loaders[$type];
  121. }
  122. /**
  123. * Add prefix path for plugin loader
  124. *
  125. * @param string $prefix
  126. * @param string $path
  127. * @param string $type
  128. * @return Zend_Form_Element_File
  129. */
  130. public function addPrefixPath($prefix, $path, $type = null)
  131. {
  132. $type = strtoupper($type);
  133. if (!empty($type) && ($type != self::TRANSFER_ADAPTER)) {
  134. return parent::addPrefixPath($prefix, $path, $type);
  135. }
  136. if (empty($type)) {
  137. $nsSeparator = (false !== strpos($prefix, '\\'))?'\\':'_';
  138. $pluginPrefix = rtrim($prefix, $nsSeparator) . $nsSeparator . 'Transfer' . $nsSeparator . 'Adapter';
  139. $pluginPath = rtrim($path, DIRECTORY_SEPARATOR) . '/Transfer/Adapter/';
  140. $loader = $this->getPluginLoader(self::TRANSFER_ADAPTER);
  141. $loader->addPrefixPath($pluginPrefix, $pluginPath);
  142. return parent::addPrefixPath($prefix, $path, null);
  143. }
  144. $loader = $this->getPluginLoader($type);
  145. $loader->addPrefixPath($prefix, $path);
  146. return $this;
  147. }
  148. /**
  149. * Set transfer adapter
  150. *
  151. * @param string|Zend_File_Transfer_Adapter_Abstract $adapter
  152. * @return Zend_Form_Element_File
  153. * @throws Zend_Form_Element_Exception
  154. */
  155. public function setTransferAdapter($adapter)
  156. {
  157. if ($adapter instanceof Zend_File_Transfer_Adapter_Abstract) {
  158. $this->_adapter = $adapter;
  159. } elseif (is_string($adapter)) {
  160. $loader = $this->getPluginLoader(self::TRANSFER_ADAPTER);
  161. $class = $loader->load($adapter);
  162. $this->_adapter = new $class;
  163. } else {
  164. require_once 'Zend/Form/Element/Exception.php';
  165. throw new Zend_Form_Element_Exception('Invalid adapter specified');
  166. }
  167. foreach (array('filter', 'validate') as $type) {
  168. $loader = $this->getPluginLoader($type);
  169. $this->_adapter->setPluginLoader($loader, $type);
  170. }
  171. return $this;
  172. }
  173. /**
  174. * Get transfer adapter
  175. *
  176. * Lazy loads HTTP transfer adapter when no adapter registered.
  177. *
  178. * @return Zend_File_Transfer_Adapter_Abstract
  179. */
  180. public function getTransferAdapter()
  181. {
  182. if (null === $this->_adapter) {
  183. $this->setTransferAdapter('Http');
  184. }
  185. return $this->_adapter;
  186. }
  187. /**
  188. * Add Validator; proxy to adapter
  189. *
  190. * @param string|Zend_Validate_Interface $validator
  191. * @param bool $breakChainOnFailure
  192. * @param mixed $options
  193. * @return Zend_Form_Element_File
  194. */
  195. public function addValidator($validator, $breakChainOnFailure = false, $options = array())
  196. {
  197. $adapter = $this->getTransferAdapter();
  198. $adapter->addValidator($validator, $breakChainOnFailure, $options, $this->getName());
  199. $this->_validated = false;
  200. return $this;
  201. }
  202. /**
  203. * Add multiple validators at once; proxy to adapter
  204. *
  205. * @param array $validators
  206. * @return Zend_Form_Element_File
  207. */
  208. public function addValidators(array $validators)
  209. {
  210. $adapter = $this->getTransferAdapter();
  211. $adapter->addValidators($validators, $this->getName());
  212. $this->_validated = false;
  213. return $this;
  214. }
  215. /**
  216. * Add multiple validators at once, overwriting; proxy to adapter
  217. *
  218. * @param array $validators
  219. * @return Zend_Form_Element_File
  220. */
  221. public function setValidators(array $validators)
  222. {
  223. $adapter = $this->getTransferAdapter();
  224. $adapter->setValidators($validators, $this->getName());
  225. $this->_validated = false;
  226. return $this;
  227. }
  228. /**
  229. * Retrieve validator by name; proxy to adapter
  230. *
  231. * @param string $name
  232. * @return Zend_Validate_Interface|null
  233. */
  234. public function getValidator($name)
  235. {
  236. $adapter = $this->getTransferAdapter();
  237. return $adapter->getValidator($name);
  238. }
  239. /**
  240. * Retrieve all validators; proxy to adapter
  241. *
  242. * @return array
  243. */
  244. public function getValidators()
  245. {
  246. $adapter = $this->getTransferAdapter();
  247. $validators = $adapter->getValidators($this->getName());
  248. if ($validators === null) {
  249. $validators = array();
  250. }
  251. return $validators;
  252. }
  253. /**
  254. * Remove validator by name; proxy to adapter
  255. *
  256. * @param string $name
  257. * @return Zend_Form_Element_File
  258. */
  259. public function removeValidator($name)
  260. {
  261. $adapter = $this->getTransferAdapter();
  262. $adapter->removeValidator($name);
  263. $this->_validated = false;
  264. return $this;
  265. }
  266. /**
  267. * Remove all validators; proxy to adapter
  268. *
  269. * @return Zend_Form_Element_File
  270. */
  271. public function clearValidators()
  272. {
  273. $adapter = $this->getTransferAdapter();
  274. $adapter->clearValidators();
  275. $this->_validated = false;
  276. return $this;
  277. }
  278. /**
  279. * Add Filter; proxy to adapter
  280. *
  281. * @param string|array $filter Type of filter to add
  282. * @param string|array $options Options to set for the filter
  283. * @return Zend_Form_Element_File
  284. */
  285. public function addFilter($filter, $options = null)
  286. {
  287. $adapter = $this->getTransferAdapter();
  288. $adapter->addFilter($filter, $options, $this->getName());
  289. return $this;
  290. }
  291. /**
  292. * Add Multiple filters at once; proxy to adapter
  293. *
  294. * @param array $filters
  295. * @return Zend_Form_Element_File
  296. */
  297. public function addFilters(array $filters)
  298. {
  299. $adapter = $this->getTransferAdapter();
  300. $adapter->addFilters($filters, $this->getName());
  301. return $this;
  302. }
  303. /**
  304. * Sets a filter for the class, erasing all previous set; proxy to adapter
  305. *
  306. * @param array $filters Filters to set
  307. * @return Zend_Form_Element_File
  308. */
  309. public function setFilters(array $filters)
  310. {
  311. $adapter = $this->getTransferAdapter();
  312. $adapter->setFilters($filters, $this->getName());
  313. return $this;
  314. }
  315. /**
  316. * Retrieve individual filter; proxy to adapter
  317. *
  318. * @param string $name
  319. * @return Zend_Filter_Interface|null
  320. */
  321. public function getFilter($name)
  322. {
  323. $adapter = $this->getTransferAdapter();
  324. return $adapter->getFilter($name);
  325. }
  326. /**
  327. * Returns all set filters; proxy to adapter
  328. *
  329. * @return array List of set filters
  330. */
  331. public function getFilters()
  332. {
  333. $adapter = $this->getTransferAdapter();
  334. $filters = $adapter->getFilters($this->getName());
  335. if ($filters === null) {
  336. $filters = array();
  337. }
  338. return $filters;
  339. }
  340. /**
  341. * Remove an individual filter; proxy to adapter
  342. *
  343. * @param string $name
  344. * @return Zend_Form_Element_File
  345. */
  346. public function removeFilter($name)
  347. {
  348. $adapter = $this->getTransferAdapter();
  349. $adapter->removeFilter($name);
  350. return $this;
  351. }
  352. /**
  353. * Remove all filters; proxy to adapter
  354. *
  355. * @return Zend_Form_Element_File
  356. */
  357. public function clearFilters()
  358. {
  359. $adapter = $this->getTransferAdapter();
  360. $adapter->clearFilters();
  361. return $this;
  362. }
  363. /**
  364. * Validate upload
  365. *
  366. * @param string $value File, can be optional, give null to validate all files
  367. * @param mixed $context
  368. * @return bool
  369. */
  370. public function isValid($value, $context = null)
  371. {
  372. if ($this->_validated) {
  373. return true;
  374. }
  375. $adapter = $this->getTransferAdapter();
  376. $translator = $this->getTranslator();
  377. if ($translator !== null) {
  378. $adapter->setTranslator($translator);
  379. }
  380. if (!$this->isRequired()) {
  381. $adapter->setOptions(array('ignoreNoFile' => true), $this->getName());
  382. } else {
  383. $adapter->setOptions(array('ignoreNoFile' => false), $this->getName());
  384. if ($this->autoInsertNotEmptyValidator() && !$this->getValidator('NotEmpty')) {
  385. $this->addValidator('NotEmpty', true);
  386. }
  387. }
  388. if($adapter->isValid($this->getName())) {
  389. $this->_validated = true;
  390. return true;
  391. }
  392. $this->_validated = false;
  393. return false;
  394. }
  395. /**
  396. * Receive the uploaded file
  397. *
  398. * @return boolean
  399. */
  400. public function receive()
  401. {
  402. if (!$this->_validated) {
  403. if (!$this->isValid($this->getName())) {
  404. return false;
  405. }
  406. }
  407. $adapter = $this->getTransferAdapter();
  408. if ($adapter->receive($this->getName())) {
  409. return true;
  410. }
  411. return false;
  412. }
  413. /**
  414. * Retrieve error codes; proxy to transfer adapter
  415. *
  416. * @return array
  417. */
  418. public function getErrors()
  419. {
  420. return parent::getErrors() + $this->getTransferAdapter()->getErrors();
  421. }
  422. /**
  423. * Are there errors registered?
  424. *
  425. * @return bool
  426. */
  427. public function hasErrors()
  428. {
  429. return (parent::hasErrors() || $this->getTransferAdapter()->hasErrors());
  430. }
  431. /**
  432. * Retrieve error messages; proxy to transfer adapter
  433. *
  434. * @return array
  435. */
  436. public function getMessages()
  437. {
  438. return parent::getMessages() + $this->getTransferAdapter()->getMessages();
  439. }
  440. /**
  441. * Set the upload destination
  442. *
  443. * @param string $path
  444. * @return Zend_Form_Element_File
  445. */
  446. public function setDestination($path)
  447. {
  448. $this->getTransferAdapter()->setDestination($path, $this->getName());
  449. return $this;
  450. }
  451. /**
  452. * Get the upload destination
  453. *
  454. * @return string
  455. */
  456. public function getDestination()
  457. {
  458. return $this->getTransferAdapter()->getDestination($this->getName());
  459. }
  460. /**
  461. * Get the final filename
  462. *
  463. * @param string $value (Optional) Element or file to return
  464. * @param boolean $path (Optional) Return also the path, defaults to true
  465. * @return string
  466. */
  467. public function getFileName($value = null, $path = true)
  468. {
  469. if (empty($value)) {
  470. $value = $this->getName();
  471. }
  472. return $this->getTransferAdapter()->getFileName($value, $path);
  473. }
  474. /**
  475. * Get internal file informations
  476. *
  477. * @param string $value (Optional) Element or file to return
  478. * @return array
  479. */
  480. public function getFileInfo($value = null)
  481. {
  482. if (empty($value)) {
  483. $value = $this->getName();
  484. }
  485. return $this->getTransferAdapter()->getFileInfo($value);
  486. }
  487. /**
  488. * Set a multifile element
  489. *
  490. * @param integer $count Number of file elements
  491. * @return Zend_Form_Element_File Provides fluent interface
  492. */
  493. public function setMultiFile($count)
  494. {
  495. if ((integer) $count < 2) {
  496. $this->setIsArray(false);
  497. $this->_counter = 1;
  498. } else {
  499. $this->setIsArray(true);
  500. $this->_counter = (integer) $count;
  501. }
  502. return $this;
  503. }
  504. /**
  505. * Returns the multifile element number
  506. *
  507. * @return integer
  508. */
  509. public function getMultiFile()
  510. {
  511. return $this->_counter;
  512. }
  513. /**
  514. * Sets the maximum file size of the form
  515. *
  516. * @return integer
  517. */
  518. public function getMaxFileSize()
  519. {
  520. if (self::$_maxFileSize < 0) {
  521. $ini = $this->_convertIniToInteger(trim(ini_get('post_max_size')));
  522. $max = $this->_convertIniToInteger(trim(ini_get('upload_max_filesize')));
  523. $min = max($ini, $max);
  524. if ($ini > 0) {
  525. $min = min($min, $ini);
  526. }
  527. if ($max > 0) {
  528. $min = min($min, $max);
  529. }
  530. self::$_maxFileSize = $min;
  531. }
  532. return self::$_maxFileSize;
  533. }
  534. /**
  535. * Sets the maximum file size of the form
  536. *
  537. * @param integer $size
  538. * @return integer
  539. */
  540. public function setMaxFileSize($size)
  541. {
  542. $ini = $this->_convertIniToInteger(trim(ini_get('post_max_size')));
  543. $max = $this->_convertIniToInteger(trim(ini_get('upload_max_filesize')));
  544. if (($max > -1) && ($size > $max)) {
  545. trigger_error("Your 'upload_max_filesize' config setting limits the maximum filesize to '$max'. You tried to set '$size'.", E_USER_NOTICE);
  546. $size = $max;
  547. }
  548. if (($ini > -1) && ($size > $ini)) {
  549. trigger_error("Your 'post_max_size' config setting limits the maximum filesize to '$ini'. You tried to set '$size'.", E_USER_NOTICE);
  550. $size = $ini;
  551. }
  552. self::$_maxFileSize = $size;
  553. return $this;
  554. }
  555. /**
  556. * Converts a ini setting to a integer value
  557. *
  558. * @param string $setting
  559. * @return integer
  560. */
  561. private function _convertIniToInteger($setting)
  562. {
  563. if (!is_numeric($setting)) {
  564. $type = strtoupper(substr($setting, -1));
  565. $setting = (integer) substr($setting, 0, -1);
  566. switch ($type) {
  567. case 'K' :
  568. $setting *= 1024;
  569. break;
  570. case 'M' :
  571. $setting *= 1024 * 1024;
  572. break;
  573. case 'G' :
  574. $setting *= 1024 * 1024 * 1024;
  575. break;
  576. default :
  577. break;
  578. }
  579. }
  580. return (integer) $setting;
  581. }
  582. /**
  583. * Set if the file will be uploaded when getting the value
  584. * This defaults to false which will force receive() when calling getValues()
  585. *
  586. * @param boolean $flag Sets if the file is handled as the elements value
  587. * @return Zend_Form_Element_File
  588. */
  589. public function setValueDisabled($flag)
  590. {
  591. $this->_valueDisabled = (bool) $flag;
  592. return $this;
  593. }
  594. /**
  595. * Returns if the file will be uploaded when calling getValues()
  596. *
  597. * @return boolean Receive the file on calling getValues()?
  598. */
  599. public function isValueDisabled()
  600. {
  601. return $this->_valueDisabled;
  602. }
  603. /**
  604. * Processes the file, returns null or the filename only
  605. * For the complete path, use getFileName
  606. *
  607. * @return null|string
  608. */
  609. public function getValue()
  610. {
  611. if ($this->_value !== null) {
  612. return $this->_value;
  613. }
  614. $content = $this->getTransferAdapter()->getFileName($this->getName());
  615. if (empty($content)) {
  616. return null;
  617. }
  618. if (!$this->isValid(null)) {
  619. return null;
  620. }
  621. if (!$this->_valueDisabled && !$this->receive()) {
  622. return null;
  623. }
  624. return $this->getFileName(null, false);
  625. }
  626. /**
  627. * Disallow setting the value
  628. *
  629. * @param mixed $value
  630. * @return Zend_Form_Element_File
  631. */
  632. public function setValue($value)
  633. {
  634. return $this;
  635. }
  636. /**
  637. * Set translator object for localization
  638. *
  639. * @param Zend_Translate|null $translator
  640. * @return Zend_Form_Element_File
  641. */
  642. public function setTranslator($translator = null)
  643. {
  644. $adapter = $this->getTransferAdapter();
  645. $adapter->setTranslator($translator);
  646. parent::setTranslator($translator);
  647. return $this;
  648. }
  649. /**
  650. * Retrieve localization translator object
  651. *
  652. * @return Zend_Translate_Adapter|null
  653. */
  654. public function getTranslator()
  655. {
  656. if ($this->translatorIsDisabled()) {
  657. return null;
  658. }
  659. $translator = $this->getTransferAdapter()->getTranslator();
  660. if (null === $translator) {
  661. require_once 'Zend/Form.php';
  662. return Zend_Form::getDefaultTranslator();
  663. }
  664. return $translator;
  665. }
  666. /**
  667. * Indicate whether or not translation should be disabled
  668. *
  669. * @param bool $flag
  670. * @return Zend_Form_Element_File
  671. */
  672. public function setDisableTranslator($flag)
  673. {
  674. $adapter = $this->getTransferAdapter();
  675. $adapter->setDisableTranslator($flag);
  676. $this->_translatorDisabled = (bool) $flag;
  677. return $this;
  678. }
  679. /**
  680. * Is translation disabled?
  681. *
  682. * @return bool
  683. */
  684. public function translatorIsDisabled()
  685. {
  686. $adapter = $this->getTransferAdapter();
  687. return $adapter->translatorIsDisabled();
  688. }
  689. /**
  690. * Was the file received?
  691. *
  692. * @return bool
  693. */
  694. public function isReceived()
  695. {
  696. $adapter = $this->getTransferAdapter();
  697. return $adapter->isReceived($this->getName());
  698. }
  699. /**
  700. * Was the file uploaded?
  701. *
  702. * @return bool
  703. */
  704. public function isUploaded()
  705. {
  706. $adapter = $this->getTransferAdapter();
  707. return $adapter->isUploaded($this->getName());
  708. }
  709. /**
  710. * Has the file been filtered?
  711. *
  712. * @return bool
  713. */
  714. public function isFiltered()
  715. {
  716. $adapter = $this->getTransferAdapter();
  717. return $adapter->isFiltered($this->getName());
  718. }
  719. /**
  720. * Returns the hash for this file element
  721. *
  722. * @param string $hash (Optional) Hash algorithm to use
  723. * @return string|array Hashstring
  724. */
  725. public function getHash($hash = 'crc32')
  726. {
  727. $adapter = $this->getTransferAdapter();
  728. return $adapter->getHash($hash, $this->getName());
  729. }
  730. /**
  731. * Returns the filesize for this file element
  732. *
  733. * @return string|array Filesize
  734. */
  735. public function getFileSize()
  736. {
  737. $adapter = $this->getTransferAdapter();
  738. return $adapter->getFileSize($this->getName());
  739. }
  740. /**
  741. * Returns the mimetype for this file element
  742. *
  743. * @return string|array Mimetype
  744. */
  745. public function getMimeType()
  746. {
  747. $adapter = $this->getTransferAdapter();
  748. return $adapter->getMimeType($this->getName());
  749. }
  750. /**
  751. * Render form element
  752. * Checks for decorator interface to prevent errors
  753. *
  754. * @param Zend_View_Interface $view
  755. * @return string
  756. * @throws Zend_Form_Element_Exception
  757. */
  758. public function render(Zend_View_Interface $view = null)
  759. {
  760. $marker = false;
  761. foreach ($this->getDecorators() as $decorator) {
  762. if ($decorator instanceof Zend_Form_Decorator_Marker_File_Interface) {
  763. $marker = true;
  764. }
  765. }
  766. if (!$marker) {
  767. require_once 'Zend/Form/Element/Exception.php';
  768. throw new Zend_Form_Element_Exception('No file decorator found... unable to render file element');
  769. }
  770. return parent::render($view);
  771. }
  772. /**
  773. * Retrieve error messages and perform translation and value substitution
  774. *
  775. * @return array
  776. */
  777. protected function _getErrorMessages()
  778. {
  779. $translator = $this->getTranslator();
  780. $messages = $this->getErrorMessages();
  781. $value = $this->getFileName();
  782. foreach ($messages as $key => $message) {
  783. if (null !== $translator) {
  784. $message = $translator->translate($message);
  785. }
  786. if ($this->isArray() || is_array($value)) {
  787. $aggregateMessages = array();
  788. foreach ($value as $val) {
  789. $aggregateMessages[] = str_replace('%value%', $val, $message);
  790. }
  791. if (!empty($aggregateMessages)) {
  792. $messages[$key] = $aggregateMessages;
  793. }
  794. } else {
  795. $messages[$key] = str_replace('%value%', $value, $message);
  796. }
  797. }
  798. return $messages;
  799. }
  800. }