File.php 23 KB

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