File.php 23 KB

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