Zend_File_Transfer-Validators.xml 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.file.transfer.validators">
  4. <title>Validators for Zend_File_Transfer</title>
  5. <para>
  6. <classname>Zend_File_Transfer</classname> is delivered with several file-related validators
  7. which can be used to increase security and prevent possible attacks. Note that these
  8. validators are only as effective as how effectively you apply them. All validators provided
  9. with <classname>Zend_File_Transfer</classname> can be found in the
  10. <classname>Zend_Validator</classname> component and are named
  11. <classname>Zend_Validate_File_*</classname>. The following validators are available:
  12. </para>
  13. <itemizedlist>
  14. <listitem>
  15. <para>
  16. <code>Count</code>: This validator checks for the number of files. A minimum and
  17. maximum range can be specified. An error will be thrown if either limit is crossed.
  18. </para>
  19. </listitem>
  20. <listitem>
  21. <para>
  22. <code>Crc32</code>: This validator checks for the crc32 hash value of the content
  23. from a file. It is based on the <code>Hash</code> validator and provides a
  24. convenient and simple validator that only supports Crc32.
  25. </para>
  26. </listitem>
  27. <listitem>
  28. <para>
  29. <code>ExcludeExtension</code>: This validator checks the extension of files. It will
  30. throw an error when an given file has a defined extension. With this validator, you
  31. can exclude defined extensions from being validated.
  32. </para>
  33. </listitem>
  34. <listitem>
  35. <para>
  36. <code>ExcludeMimeType</code>: This validator validates the MIME type of files. It
  37. can also validate MIME types and will throw an error if the MIME type of specified
  38. file matches.
  39. </para>
  40. </listitem>
  41. <listitem>
  42. <para>
  43. <code>Exists</code>: This validator checks for the existence of files. It will throw
  44. an error when a specified file does not exist.
  45. </para>
  46. </listitem>
  47. <listitem>
  48. <para>
  49. <code>Extension</code>: This validator checks the extension of files. It will throw
  50. an error when a specified file has an undefined extension.
  51. </para>
  52. </listitem>
  53. <listitem>
  54. <para>
  55. <code>FilesSize</code>: This validator checks the size of validated files.
  56. It remembers internally the size of all checked files and throws an error when the
  57. sum of all specified files exceed the defined size. It also provides minimum and
  58. maximum values.
  59. </para>
  60. </listitem>
  61. <listitem>
  62. <para>
  63. <code>ImageSize</code>: This validator checks the size of image. It validates the
  64. width and height and enforces minimum and maximum dimensions.
  65. </para>
  66. </listitem>
  67. <listitem>
  68. <para>
  69. <code>IsCompressed</code>: This validator checks whether the file is compressed. It
  70. is based on the <code>MimeType</code> validator and validates for compression
  71. archives like zip or arc. You can also limit it to other archives.
  72. </para>
  73. </listitem>
  74. <listitem>
  75. <para>
  76. <code>IsImage</code>: This validator checks whether the file is an image. It is
  77. based on the <code>MimeType</code> validator and validates for image files like jpg
  78. or gif. You can also limit it to other image types.
  79. </para>
  80. </listitem>
  81. <listitem>
  82. <para>
  83. <code>Hash</code>: This validator checks the hash value of the content from a file.
  84. It supports multiple algorithms.
  85. </para>
  86. </listitem>
  87. <listitem>
  88. <para>
  89. <code>Md5</code>: This validator checks for the md5 hash value of the content from a
  90. file. It is based on the <code>Hash</code> validator and provides a convenient and
  91. simple validator that only supports Md5.
  92. </para>
  93. </listitem>
  94. <listitem>
  95. <para>
  96. <code>MimeType</code>: This validator validates the MIME type of files. It can also
  97. validate MIME types and will throw an error if the MIME type of a specified
  98. file does not match.
  99. </para>
  100. </listitem>
  101. <listitem>
  102. <para>
  103. <code>NotExists</code>: This validator checks for the existence of files. It will
  104. throw an error when an given file does exist.
  105. </para>
  106. </listitem>
  107. <listitem>
  108. <para>
  109. <code>Sha1</code>: This validator checks for the sha1 hash value of the content from
  110. a file. It is based on the <code>Hash</code> validator and provides a convenient and
  111. simple validator that only supports sha1.
  112. </para>
  113. </listitem>
  114. <listitem>
  115. <para>
  116. <code>Size</code>: This validator is able to check files for its file size. It
  117. provides a minimum and maximum size range and will throw an error when either of
  118. these thesholds are crossed.
  119. </para>
  120. </listitem>
  121. <listitem>
  122. <para>
  123. <code>Upload</code>: This validator is internal. It checks if an upload has
  124. resulted in an error. You must not set it, as it's automatically set by
  125. <classname>Zend_File_Transfer</classname> itself. So you do not use this validator
  126. directly. You should only know that it exists.
  127. </para>
  128. </listitem>
  129. <listitem>
  130. <para>
  131. <code>WordCount</code>: This validator is able to check the number of words within
  132. files. It provides a minimum and maximum count and will throw an error when either
  133. of these thresholds are crossed.
  134. </para>
  135. </listitem>
  136. </itemizedlist>
  137. <sect2 id="zend.file.transfer.validators.usage">
  138. <title>Using Validators with Zend_File_Transfer</title>
  139. <para>
  140. Putting validators to work is quite simple. There are several methods for adding and
  141. manipulating validators:
  142. </para>
  143. <itemizedlist>
  144. <listitem>
  145. <para>
  146. <code>isValid($files = null)</code>: Checks the specified files using all
  147. validators. <code>$files</code> may be either a real filename, the element's
  148. name or the name of the temporary file.
  149. </para>
  150. </listitem>
  151. <listitem>
  152. <para>
  153. <code>addValidator($validator, $breakChainOnFailure, $options = null, $files =
  154. null)</code>: Adds the specified validator to the validator
  155. stack (optionally only to the file(s) specified).
  156. <code>$validator</code> may be either an actual validator
  157. instance or a short name specifying the validator type (e.g., 'Count').
  158. </para>
  159. </listitem>
  160. <listitem>
  161. <para>
  162. <code>addValidators(array $validators, $files =
  163. null)</code>: Adds the specified validators to the stack of
  164. validators. Each entry may be either a validator
  165. type/options pair or an array with the key 'validator'
  166. specifying the validator. All other options will be
  167. considered validator options for instantiation.
  168. </para>
  169. </listitem>
  170. <listitem>
  171. <para>
  172. <code>setValidators(array $validators, $files =
  173. null)</code>: Overwrites any existing validators with
  174. the validators specified. The validators should follow the
  175. syntax for <code>addValidators()</code>.
  176. </para>
  177. </listitem>
  178. <listitem>
  179. <para>
  180. <code>hasValidator($name)</code>: Indicates whether a
  181. validator has been registered.
  182. </para>
  183. </listitem>
  184. <listitem>
  185. <para>
  186. <code>getValidator($name)</code>: Returns a previously
  187. registered validator.
  188. </para>
  189. </listitem>
  190. <listitem>
  191. <para>
  192. <code>getValidators($files = null)</code>: Returns
  193. registered validators. If <code>$files</code> is specified,
  194. returns validators for that particular file or set of
  195. files.
  196. </para>
  197. </listitem>
  198. <listitem>
  199. <para>
  200. <code>removeValidator($name)</code>: Removes a previously
  201. registered validator.
  202. </para>
  203. </listitem>
  204. <listitem>
  205. <para>
  206. <code>clearValidators()</code>: Clears all
  207. registered validators.
  208. </para>
  209. </listitem>
  210. </itemizedlist>
  211. <example id="zend.file.transfer.validators.usage.example">
  212. <title>Add Validators to a File Transfer Object</title>
  213. <programlisting language="php"><![CDATA[
  214. $upload = new Zend_File_Transfer();
  215. // Set a file size with 20000 bytes
  216. $upload->addValidator('Size', false, 20000);
  217. // Set a file size with 20 bytes minimum and 20000 bytes maximum
  218. $upload->addValidator('Size', false, array('min' => 20, 'max' => 20000));
  219. // Set a file size with 20 bytes minimum and 20000 bytes maximum and
  220. // a file count in one step
  221. $upload->setValidators(array(
  222. 'Size' => array('min' => 20, 'max' => 20000),
  223. 'Count' => array('min' => 1, 'max' => 3),
  224. ));
  225. ]]></programlisting>
  226. </example>
  227. <example id="zend.file.transfer.validators.usage.exampletwo">
  228. <title>Limit Validators to Single Files</title>
  229. <para>
  230. <code>addValidator()</code>, <code>addValidators()</code>, and
  231. <code>setValidators()</code> each accept a final
  232. <code>$files</code> argument. This argument can be used to
  233. specify a particular file or array of files on which to set the
  234. given validator.
  235. </para>
  236. <programlisting language="php"><![CDATA[
  237. $upload = new Zend_File_Transfer();
  238. // Set a file size with 20000 bytes and limits it only to 'file2'
  239. $upload->addValidator('Size', false, 20000, 'file2');
  240. ]]></programlisting>
  241. </example>
  242. <para>
  243. Normally, you should use the <code>addValidators()</code> method, which can be called
  244. multiple times.
  245. </para>
  246. <example id="zend.file.transfer.validators.usage.examplemultiple">
  247. <title>Add Multiple Validators</title>
  248. <para>
  249. Often it's simpler just to call <code>addValidator()</code> multiple times with one
  250. call for each validator. This also increases readability and makes your code more
  251. maintainable. All methods provide a fluent interface, so you can couple the calls as
  252. shown below:
  253. </para>
  254. <programlisting language="php"><![CDATA[
  255. $upload = new Zend_File_Transfer();
  256. // Set a file size with 20000 bytes
  257. $upload->addValidator('Size', false, 20000)
  258. ->addValidator('Count', false, 2)
  259. ->addValidator('Filessize', false, 25000);
  260. ]]></programlisting>
  261. </example>
  262. <note>
  263. <para>
  264. Note that setting the same validator
  265. multiple times is allowed, but doing so can lead to issues when using
  266. different options for the same validator.
  267. </para>
  268. </note>
  269. <para>
  270. Last but not least, you can simply check the files using <code>isValid()</code>.
  271. </para>
  272. <example id="zend.file.transfer.validators.usage.exampleisvalid">
  273. <title>Validate the Files</title>
  274. <para>
  275. <code>isValid()</code> accepts the file name of the uploaded or downloaded file, the
  276. temporary file name and or the name of the form element. If no parameter or null is
  277. given all files will be validated
  278. </para>
  279. <programlisting language="php"><![CDATA[
  280. $upload = new Zend_File_Transfer();
  281. // Set a file size with 20000 bytes
  282. $upload->addValidator('Size', false, 20000)
  283. ->addValidator('Count', false, 2)
  284. ->addValidator('Filessize', false, 25000);
  285. if ($upload->isValid()) {
  286. print "Validation failure";
  287. }
  288. ]]></programlisting>
  289. </example>
  290. <note>
  291. <para>
  292. Note that <code>isValid()</code> will be called automatically when you receive the
  293. files and have not called it previously.
  294. </para>
  295. </note>
  296. <para>
  297. When validation has failed it is a good idea to get information about the
  298. problems found. To get this information, you can use the methods
  299. <code>getMessages()</code> which returns all validation messages as array,
  300. <code>getErrors()</code> which returns all error codes, and <code>hasErrors()</code>
  301. which returns true as soon as a validation error has been found.
  302. </para>
  303. </sect2>
  304. <sect2 id="zend.file.transfer.validators.count">
  305. <title>Count Validator</title>
  306. <para>
  307. The <code>Count</code> validator checks for the number of files which are provided. It
  308. supports the following option keys:
  309. </para>
  310. <itemizedlist>
  311. <listitem>
  312. <para>
  313. <code>min</code>: Sets the minimum number of files to transfer.
  314. </para>
  315. <note>
  316. <para>
  317. When using this option you must give the minimum number of files when
  318. calling this validator the first time; otherwise you will get an error in
  319. return.
  320. </para>
  321. </note>
  322. <para>
  323. With this option you can define the minimum number of files you expect to
  324. receive.
  325. </para>
  326. </listitem>
  327. <listitem>
  328. <para>
  329. <code>max</code>: Sets the maximum number of files to transfer.
  330. </para>
  331. <para>
  332. With this option you can limit the number of files which are accepted but also
  333. detect a possible attack when more files are given than defined in your form.
  334. </para>
  335. </listitem>
  336. </itemizedlist>
  337. <para>
  338. If you initiate this validator with a string or integer, the value will be used as
  339. <code>max</code>. Or you can also use the methods <code>setMin()</code> and
  340. <code>setMax()</code> to set both options afterwards and <code>getMin()</code> and
  341. <code>getMax()</code> to retrieve the actual set values.
  342. </para>
  343. <example id="zend.file.transfer.validators.count.example">
  344. <title>Using the Count Validator</title>
  345. <programlisting language="php"><![CDATA[
  346. $upload = new Zend_File_Transfer();
  347. // Limit the amount of files to maximum 2
  348. $upload->addValidator('Count', false, 2);
  349. // Limit the amount of files to maximum 5 and minimum 1 file
  350. $upload->addValidator('Count', false, array('min' =>1, 'max' => 5));
  351. ]]></programlisting>
  352. </example>
  353. <note>
  354. <para>
  355. Note that this validator stores the number of checked files internally. The file
  356. which exceeds the maximum will be returned as error.
  357. </para>
  358. </note>
  359. </sect2>
  360. <sect2 id="zend.file.transfer.validators.crc32">
  361. <title>Crc32 Validator</title>
  362. <para>
  363. The <code>Crc32</code> validator checks the content of a transferred file by hashing it.
  364. This validator uses the hash extension from PHP with the crc32 algorithm.
  365. It supports the following options:
  366. </para>
  367. <itemizedlist>
  368. <listitem>
  369. <para>
  370. <code>*</code>: Sets any key or use a numeric array. The values will be used
  371. as hash to validate against.
  372. </para>
  373. <para>
  374. You can set multiple hashes by using different keys. Each will be checked and
  375. the validation will fail only if all values fail.
  376. </para>
  377. </listitem>
  378. </itemizedlist>
  379. <example id="zend.file.transfer.validators.crc32.example">
  380. <title>Using the Crc32 Validator</title>
  381. <programlisting language="php"><![CDATA[
  382. $upload = new Zend_File_Transfer();
  383. // Checks whether the content of the uploaded file has the given hash
  384. $upload->addValidator('Crc32', false, '3b3652f');
  385. // Limits this validator to two different hashes
  386. $upload->addValidator('Crc32', false, array('3b3652f', 'e612b69'));
  387. ]]></programlisting>
  388. </example>
  389. </sect2>
  390. <sect2 id="zend.file.transfer.validators.excludeextension">
  391. <title>ExcludeExtension Validator</title>
  392. <para>
  393. The <code>ExcludeExtension</code> validator checks the file extension of the specified
  394. files. It supports the following options:
  395. </para>
  396. <itemizedlist>
  397. <listitem>
  398. <para>
  399. <code>*</code>: Sets any key or use a numeric array. The values will be used
  400. to check whether the given file does not use this file extension.
  401. </para>
  402. </listitem>
  403. <listitem>
  404. <para>
  405. <code>case</code>: Sets a boolean indicating whether validation should be
  406. case-sensitive. The default is not case sensitive. Note that this key can be
  407. applied to for all available extensions.
  408. </para>
  409. </listitem>
  410. </itemizedlist>
  411. <para>
  412. This validator accepts multiple extensions, either as a comma-delimited string, or as an
  413. array. You may also use the methods <code>setExtension()</code>,
  414. <code>addExtension()</code>, and <code>getExtension()</code> to set and retrieve
  415. extensions.
  416. </para>
  417. <para>
  418. In some cases it is useful to match in a case-sensitive fashion. So the constructor
  419. allows a second parameter called <code>$case</code> which, if set to true, validates the
  420. extension by comparing it with the specified values in a case-sensitive fashion.
  421. </para>
  422. <example id="zend.file.transfer.validators.excludeextension.example">
  423. <title>Using the ExcludeExtension Validator</title>
  424. <programlisting language="php"><![CDATA[
  425. $upload = new Zend_File_Transfer();
  426. // Do not allow files with extension php or exe
  427. $upload->addValidator('ExcludeExtension', false, 'php,exe');
  428. // Do not allow files with extension php or exe, but use array notation
  429. $upload->addValidator('ExcludeExtension', false, array('php', 'exe'));
  430. // Check in a case-sensitive fashion
  431. $upload->addValidator('ExcludeExtension',
  432. false,
  433. array('php', 'exe', 'case' => true));
  434. $upload->addValidator('ExcludeExtension',
  435. false,
  436. array('php', 'exe', 'case' => true));
  437. ]]></programlisting>
  438. </example>
  439. <note>
  440. <para>
  441. Note that this validator only checks the file extension. It does not check the
  442. file's MIME type.
  443. </para>
  444. </note>
  445. </sect2>
  446. <sect2 id="zend.file.transfer.validators.excludemimetype">
  447. <title>ExcludeMimeType Validator</title>
  448. <para>
  449. The <code>ExcludeMimeType</code> validator checks the MIME type of transferred files.
  450. It supports the following options:
  451. </para>
  452. <itemizedlist>
  453. <listitem>
  454. <para>
  455. <code>*</code>: Sets any key individually or use a numeric array. Sets the MIME
  456. type to validate against.
  457. </para>
  458. <para>
  459. With this option you can define the MIME type of files that are not to be
  460. accepted.
  461. </para>
  462. </listitem>
  463. </itemizedlist>
  464. <para>
  465. This validator accepts multiple MIME types, either as a comma-delimited string, or as an
  466. array. You may also use the methods <code>setMimeType()</code>,
  467. <code>addMimeType()</code>, and <code>getMimeType()</code> to set and retrieve the MIME
  468. types.
  469. </para>
  470. <example id="zend.file.transfer.validators.excludemimetype.example">
  471. <title>Using the ExcludeMimeType Validator</title>
  472. <programlisting language="php"><![CDATA[
  473. $upload = new Zend_File_Transfer();
  474. // Does not allow MIME type of gif images for all files
  475. $upload->addValidator('ExcludeMimeType', false, 'image/gif');
  476. // Does not allow MIME type of gif and jpg images for all given files
  477. $upload->addValidator('ExcludeMimeType', false, array('image/gif',
  478. 'image/jpeg');
  479. // Does not allow MIME type of the group images for all given files
  480. $upload->addValidator('ExcludeMimeType', false, 'image');
  481. ]]></programlisting>
  482. </example>
  483. <para>
  484. The above example shows that it is also possible to disallow groups of MIME types.
  485. For example, to disallow all images, just use 'image' as the MIME type. This can be used
  486. for all groups of MIME types like 'image', 'audio', 'video', 'text', etc.
  487. </para>
  488. <note>
  489. <para>
  490. Note that disallowing groups of MIME types will disallow all members of this group
  491. even if this is not intentional. When you disallow 'image' you will disallow all
  492. types of images like 'image/jpeg' or 'image/vasa'. When you are not sure if you want
  493. to disallow all types, you should disallow only specific MIME types instead of
  494. complete groups.
  495. </para>
  496. </note>
  497. </sect2>
  498. <sect2 id="zend.file.transfer.validators.exists">
  499. <title>Exists Validator</title>
  500. <para>
  501. The <code>Exists</code> validator checks for the existence of specified files. It
  502. supports the following options:
  503. </para>
  504. <itemizedlist>
  505. <listitem>
  506. <para>
  507. <code>*</code>: Sets any key or use a numeric array to check if the specific
  508. file exists in the given directory.
  509. </para>
  510. </listitem>
  511. </itemizedlist>
  512. <para>
  513. This validator accepts multiple directories, either as a comma-delimited string, or as
  514. an array. You may also use the methods <code>setDirectory()</code>,
  515. <code>addDirectory()</code>, and <code>getDirectory()</code> to set and retrieve
  516. directories.
  517. </para>
  518. <example id="zend.file.transfer.validators.exists.example">
  519. <title>Using the Exists Validator</title>
  520. <programlisting language="php"><![CDATA[
  521. $upload = new Zend_File_Transfer();
  522. // Add the temp directory to check for
  523. $upload->addValidator('Exists', false, '\temp');
  524. // Add two directories using the array notation
  525. $upload->addValidator('Exists',
  526. false,
  527. array('\home\images', '\home\uploads'));
  528. ]]></programlisting>
  529. </example>
  530. <note>
  531. <para>
  532. Note that this validator checks whether the specified file exists in all of the
  533. given directories. The validation will fail if the file does not exist in any of the
  534. given directories.
  535. </para>
  536. </note>
  537. </sect2>
  538. <sect2 id="zend.file.transfer.validators.extension">
  539. <title>Extension Validator</title>
  540. <para>
  541. The <code>Extension</code> validator checks the file extension of the specified files.
  542. It supports the following options:
  543. </para>
  544. <itemizedlist>
  545. <listitem>
  546. <para>
  547. <code>*</code>: Sets any key or use a numeric array to check whether the
  548. specified file has this file extension.
  549. </para>
  550. </listitem>
  551. <listitem>
  552. <para>
  553. <code>case</code>: Sets whether validation should be done in a case-sensitive
  554. fashion. The default is no case sensitivity. Note the this key is used for all
  555. given extensions.
  556. </para>
  557. </listitem>
  558. </itemizedlist>
  559. <para>
  560. This validator accepts multiple extensions, either as a comma-delimited string, or as an
  561. array. You may also use the methods <code>setExtension()</code>,
  562. <code>addExtension()</code>, and <code>getExtension()</code> to set and retrieve
  563. extension values.
  564. </para>
  565. <para>
  566. In some cases it is useful to test in a case-sensitive fashion. Therefore the
  567. constructor takes a second parameter <code>$case</code>, which, if set to true, will
  568. validate the extension in a case-sensitive fashion.
  569. </para>
  570. <example id="zend.file.transfer.validators.extension.example">
  571. <title>Using the Extension Validator</title>
  572. <programlisting language="php"><![CDATA[
  573. $upload = new Zend_File_Transfer();
  574. // Limit the extensions to jpg and png files
  575. $upload->addValidator('Extension', false, 'jpg,png');
  576. // Limit the extensions to jpg and png files but use array notation
  577. $upload->addValidator('Extension', false, array('jpg', 'png'));
  578. // Check case sensitive
  579. $upload->addValidator('Extension', false, array('mo', 'png', 'case' => true));
  580. if (!$upload->isValid('C:\temp\myfile.MO')) {
  581. print 'Not valid because MO and mo do not match with case sensitivity;
  582. }
  583. ]]></programlisting>
  584. </example>
  585. <note>
  586. <para>
  587. Note that this validator only checks the file extension. It does not check the
  588. file's MIME type.
  589. </para>
  590. </note>
  591. </sect2>
  592. <sect2 id="zend.file.transfer.validators.filessize">
  593. <title>FilesSize Validator</title>
  594. <para>
  595. The <code>FilesSize</code> validator checks for the aggregate size of all transferred
  596. files. It supports the following options:
  597. </para>
  598. <itemizedlist>
  599. <listitem>
  600. <para>
  601. <code>min</code>: Sets the minimum aggregate file size.
  602. This option defines the minimum aggregate file size to be transferred.
  603. </para>
  604. </listitem>
  605. <listitem>
  606. <para>
  607. <code>max</code>: Sets the maximum aggregate file size.
  608. </para>
  609. <para>
  610. This option limits the aggregate file size of all transferred files, but not the
  611. file size of individual files.
  612. </para>
  613. </listitem>
  614. <listitem>
  615. <para>
  616. <code>bytestring</code>: Defines whether a failure is to return a user-friendly
  617. number or the plain file size.
  618. </para>
  619. <para>
  620. This option defines whether the user sees '10864' or '10MB'. The default value
  621. is true, so '10MB' is returned if you did not specify otherwise.
  622. </para>
  623. </listitem>
  624. </itemizedlist>
  625. <para>
  626. You can initialize this validator with a string, which will then be used to set the
  627. <code>max</code> option. You can also use the methods <code>setMin()</code> and
  628. <code>setMax()</code> to set both options after construction, along with
  629. <code>getMin()</code> and <code>getMax()</code> to retrieve the values that have been
  630. set previously.
  631. </para>
  632. <para>
  633. The size itself is also accepted in SI notation as handled by most operating systems.
  634. That is, instead of specifying <emphasis>20000 bytes</emphasis>,
  635. <emphasis>20kB</emphasis> may be given. All file sizes are converted using 1024 as the
  636. base value. The following Units are accepted: <code>kB</code>, <code>MB</code>,
  637. <code>GB</code>, <code>TB</code>, <code>PB</code> and <code>EB</code>. Note that 1kB is
  638. equal to 1024 bytes, 1MB is equal to 1024kB, and so on.
  639. </para>
  640. <example id="zend.file.transfer.validators.filessize.example">
  641. <title>Using the FilesSize Validator</title>
  642. <programlisting language="php"><![CDATA[
  643. $upload = new Zend_File_Transfer();
  644. // Limit the size of all files to be uploaded to 40000 bytes
  645. $upload->addValidator('FilesSize', false, 40000);
  646. // Limit the size of all files to be uploaded to maximum 4MB and mimimum 10kB
  647. $upload->addValidator('FilesSize',
  648. false,
  649. array('min' => '10kB', 'max' => '4MB'));
  650. // As before, but returns the plain file size instead of a user-friendly string
  651. $upload->addValidator('FilesSize',
  652. false,
  653. array('min' => '10kB',
  654. 'max' => '4MB',
  655. 'bytestring' => false));
  656. ]]></programlisting>
  657. </example>
  658. <note>
  659. <para>
  660. Note that this validator internally stores the file size of checked files. The file
  661. which exceeds the size will be returned as an error.
  662. </para>
  663. </note>
  664. </sect2>
  665. <sect2 id="zend.file.transfer.validators.imagesize">
  666. <title>ImageSize Validator</title>
  667. <para>
  668. The <code>ImageSize</code> validator checks the size of image files.
  669. It supports the following options:
  670. </para>
  671. <itemizedlist>
  672. <listitem>
  673. <para>
  674. <code>minheight</code>: Sets the minimum image height.
  675. </para>
  676. </listitem>
  677. <listitem>
  678. <para>
  679. <code>maxheight</code>: Sets the maximum image height.
  680. </para>
  681. </listitem>
  682. <listitem>
  683. <para>
  684. <code>minwidth</code>: Sets the minimum image width.
  685. </para>
  686. </listitem>
  687. <listitem>
  688. <para>
  689. <code>maxwidth</code>: Sets the maximum image width.
  690. </para>
  691. </listitem>
  692. </itemizedlist>
  693. <para>
  694. The methods <code>setImageMin()</code> and <code>setImageMax()</code> also set
  695. both minimum and maximum values, while the methods <code>getMin()</code> and
  696. <code>getMax()</code> return the currently set values.
  697. </para>
  698. <para>
  699. For your convenience there are also the <code>setImageWidth()</code> and
  700. <code>setImageHeight()</code> methods, which set the minimum and maximum height and
  701. width of the image file. They, too, have corresponding <code>getImageWidth()</code> and
  702. <code>getImageHeight()</code> methods to retrieve the currently set values.
  703. </para>
  704. <para>
  705. To bypass validation of a particular dimension, the relevent option simply should not be
  706. set.
  707. </para>
  708. <example id="zend.file.transfer.validators.imagesize.example">
  709. <title>Using the ImageSize Validator</title>
  710. <programlisting language="php"><![CDATA[
  711. $upload = new Zend_File_Transfer();
  712. // Limit the size of a image to a height of 100-200 and a width of
  713. // 40-80 pixel
  714. $upload->addValidator('ImageSize', false,
  715. array('minwidth' => 40,
  716. 'maxwidth' => 80,
  717. 'minheight' => 100,
  718. 'maxheight' => 200)
  719. );
  720. // Reset the width for validation
  721. $upload->setImageWidth(array('minwidth' => 20, 'maxwidth' => 200));
  722. ]]></programlisting>
  723. </example>
  724. </sect2>
  725. <sect2 id="zend.file.transfer.validators.iscompressed">
  726. <title>IsCompressed Validator</title>
  727. <para>
  728. The <code>IsCompressed</code> validator checks if a transferred file is a compressed
  729. archive, such as zip or arc. This validator is based on the <code>MimeType</code>
  730. validator and supports the same methods and options. You may also limit this validator
  731. to particular compression types with the methods described there.
  732. </para>
  733. <example id="zend.file.transfer.validators.iscompressed.example">
  734. <title>Using the IsCompressed Validator</title>
  735. <programlisting language="php"><![CDATA[
  736. $upload = new Zend_File_Transfer();
  737. // Checks is the uploaded file is a compressed archive
  738. $upload->addValidator('IsCompressed', false);
  739. // Limits this validator to zip files only
  740. $upload->addValidator('IsCompressed', false, array('application/zip'));
  741. // Limits this validator to zip files only using simpler notation
  742. $upload->addValidator('IsCompressed', false, 'zip');
  743. ]]></programlisting>
  744. </example>
  745. <note>
  746. <para>
  747. Note that there is no check if you set a MIME type that is not a archive. For
  748. example, it would be possible to define gif files to be accepted by this validator.
  749. Using the 'MimeType' validator for files which are not archived will result in more
  750. readable code.
  751. </para>
  752. </note>
  753. </sect2>
  754. <sect2 id="zend.file.transfer.validators.isimage">
  755. <title>IsImage Validator</title>
  756. <para>
  757. The <code>IsImage</code> validator checks if a transferred file is a image file, such as
  758. gif or jpeg. This validator is based on the <code>MimeType</code> validator and supports
  759. the same methods and options. You can limit this validator to particular image types
  760. with the methods described there.
  761. </para>
  762. <example id="zend.file.transfer.validators.isimage.example">
  763. <title>Using the IsImage Validator</title>
  764. <programlisting language="php"><![CDATA[
  765. $upload = new Zend_File_Transfer();
  766. // Checks whether the uploaded file is a image file
  767. $upload->addValidator('IsImage', false);
  768. // Limits this validator to gif files only
  769. $upload->addValidator('IsImage', false, array('application/gif'));
  770. // Limits this validator to jpeg files only using a simpler notation
  771. $upload->addValidator('IsImage', false, 'jpeg');
  772. ]]></programlisting>
  773. </example>
  774. <note>
  775. <para>
  776. Note that there is no check if you set a MIME type that is not an image. For
  777. example, it would be possible to define zip files to be accepted by this validator.
  778. Using the 'MimeType' validator for files which are not images will result in more
  779. readable code.
  780. </para>
  781. </note>
  782. </sect2>
  783. <sect2 id="zend.file.transfer.validators.hash">
  784. <title>Hash Validator</title>
  785. <para>
  786. The <code>Hash</code> validator checks the content of a transferred file by hashing it.
  787. This validator uses the hash extension from PHP. It supports the following options:
  788. </para>
  789. <itemizedlist>
  790. <listitem>
  791. <para>
  792. <code>*</code>: Takes any key or use a numeric array. Sets the hash to validate
  793. against.
  794. </para>
  795. <para>
  796. You can set multiple hashes by passing them as an array. Each file is checked,
  797. and the validation will fail only if all files fail validation.
  798. </para>
  799. </listitem>
  800. <listitem>
  801. <para>
  802. <code>algorithm</code>: Sets the algorithm to use for hashing the content.
  803. </para>
  804. <para>
  805. You can set multiple algorithm by calling the <code>addHash()</code> method
  806. multiple times.
  807. </para>
  808. </listitem>
  809. </itemizedlist>
  810. <example id="zend.file.transfer.validators.hash.example">
  811. <title>Using the Hash Validator</title>
  812. <programlisting language="php"><![CDATA[
  813. $upload = new Zend_File_Transfer();
  814. // Checks if the content of the uploaded file contains the given hash
  815. $upload->addValidator('Hash', false, '3b3652f');
  816. // Limits this validator to two different hashes
  817. $upload->addValidator('Hash', false, array('3b3652f', 'e612b69'));
  818. // Sets a different algorithm to check against
  819. $upload->addValidator('Hash',
  820. false,
  821. array('315b3cd8273d44912a7',
  822. 'algorithm' => 'md5'));
  823. ]]></programlisting>
  824. </example>
  825. <note>
  826. <para>
  827. This validator supports about 34 different hash algorithms. The most common include
  828. 'crc32', 'md5' and 'sha1'. A comprehesive list of supports hash algorithms can be
  829. found at the <ulink url="http://php.net/hash_algos">hash_algos method</ulink> on the
  830. <ulink url="http://php.net">php.net site</ulink>.
  831. </para>
  832. </note>
  833. </sect2>
  834. <sect2 id="zend.file.transfer.validators.md5">
  835. <title>Md5 Validator</title>
  836. <para>
  837. The <code>Md5</code> validator checks the content of a transferred file by hashing it.
  838. This validator uses the hash extension for PHP with the md5 algorithm.
  839. It supports the following options:
  840. </para>
  841. <itemizedlist>
  842. <listitem>
  843. <para>
  844. <code>*</code>: Takes any key or use a numeric array.
  845. </para>
  846. <para>
  847. You can set multiple hashes by passing them as an array. Each file is checked,
  848. and the validation will fail only if all files fail validation.
  849. </para>
  850. </listitem>
  851. </itemizedlist>
  852. <example id="zend.file.transfer.validators.md5.example">
  853. <title>Using the Md5 Validator</title>
  854. <programlisting language="php"><![CDATA[
  855. $upload = new Zend_File_Transfer();
  856. // Checks if the content of the uploaded file has the given hash
  857. $upload->addValidator('Md5', false, '3b3652f336522365223');
  858. // Limits this validator to two different hashes
  859. $upload->addValidator('Md5',
  860. false,
  861. array('3b3652f336522365223',
  862. 'eb3365f3365ddc65365'));
  863. ]]></programlisting>
  864. </example>
  865. </sect2>
  866. <sect2 id="zend.file.transfer.validators.mimetype">
  867. <title>MimeType Validator</title>
  868. <para>
  869. The <code>MimeType</code> validator checks the MIME type of transferred files.
  870. It supports the following options:
  871. </para>
  872. <itemizedlist>
  873. <listitem>
  874. <para>
  875. <code>*</code>: Sets any key or use a numeric array. Sets the MIME type type to
  876. validate against.
  877. </para>
  878. <para>
  879. Defines the MIME type of files to be accepted.
  880. </para>
  881. </listitem>
  882. <listitem>
  883. <para>
  884. <code>magicfile</code>: The magicfile to be used.
  885. </para>
  886. <para>
  887. With this option you can define which magicfile to use. When it's not set or
  888. empty, the MAGIC constant will be used instead. This option is available since
  889. Zend Framework 1.7.1.
  890. </para>
  891. </listitem>
  892. </itemizedlist>
  893. <para>
  894. This validator accepts multiple MIME type, either as a comma-delimited string, or as an
  895. array. You may also use the methods <code>setMimeType()</code>,
  896. <code>addMimeType()</code>, and <code>getMimeType()</code> to set and retrieve MIME
  897. type.
  898. </para>
  899. <para>
  900. You can also set the magicfile which shall be used by fileinfo with the 'magicfile'
  901. option. Additionally there are convenient <code>setMagicFile()</code> and
  902. <code>getMagicFile()</code> methods which allow later setting and retrieving of the
  903. magicfile parameter. This methods are available since Zend Framework 1.7.1.
  904. </para>
  905. <example id="zend.file.transfer.validators.mimetype.example">
  906. <title>Using the MimeType Validator</title>
  907. <programlisting language="php"><![CDATA[
  908. $upload = new Zend_File_Transfer();
  909. // Limit the MIME type of all given files to gif images
  910. $upload->addValidator('MimeType', false, 'image/gif');
  911. // Limit the MIME type of all given files to gif and jpeg images
  912. $upload->addValidator('MimeType', false, array('image/gif', 'image/jpeg');
  913. // Limit the MIME type of all given files to the group images
  914. $upload->addValidator('MimeType', false, 'image');
  915. // Use a different magicfile
  916. $upload->addValidator('MimeType',
  917. false,
  918. array('image',
  919. 'magicfile' => '/path/to/magicfile.mgx'));
  920. ]]></programlisting>
  921. </example>
  922. <para>
  923. The above example shows that it is also possible to limit the accepted MIME type to a
  924. group of MIME types. To allow all images just use 'image' as MIME type. This can be used
  925. for all groups of MIME types like 'image', 'audio', 'video', 'text, and so on.
  926. </para>
  927. <note>
  928. <para>
  929. Note that allowing groups of MIME types will accept all members of this group even
  930. if your application does not support them. When you allow 'image' you will also get
  931. 'image/xpixmap' or 'image/vasa' which could be problematic. When you are not sure if
  932. your application supports all types you should better allow only defined MIME types
  933. instead of the complete group.
  934. </para>
  935. </note>
  936. <note>
  937. <para>
  938. This component will use the <code>fileinfo</code> extension if it is available. If
  939. it's not, it will degrade to the <code>mime_content_type</code> function. And if the
  940. function call fails it will use the MIME type which is given by HTTP.
  941. </para>
  942. <para>
  943. You should be aware of possible security problems when you have whether
  944. <code>fileinfo</code> nor <code>mime_content_type</code> available. The MIME type
  945. given by HTTP is not secure and can be easily manipulated.
  946. </para>
  947. </note>
  948. </sect2>
  949. <sect2 id="zend.file.transfer.validators.notexists">
  950. <title>NotExists Validator</title>
  951. <para>
  952. The <code>NotExists</code> validator checks for the existence of the provided files. It
  953. supports the following options:
  954. </para>
  955. <itemizedlist>
  956. <listitem>
  957. <para>
  958. <code>*</code>: Set any key or use a numeric array. Checks whether the file
  959. exists in the given directory.
  960. </para>
  961. </listitem>
  962. </itemizedlist>
  963. <para>
  964. This validator accepts multiple directories either as a comma-delimited string, or as an
  965. array. You may also use the methods <code>setDirectory()</code>,
  966. <code>addDirectory()</code>, and <code>getDirectory()</code> to set and retrieve
  967. directories.
  968. </para>
  969. <example id="zend.file.transfer.validators.notexists.example">
  970. <title>Using the NotExists Validator</title>
  971. <programlisting language="php"><![CDATA[
  972. $upload = new Zend_File_Transfer();
  973. // Add the temp directory to check
  974. $upload->addValidator('NotExists', false, '\temp');
  975. // Add two directories using the array notation
  976. $upload->addValidator('NotExists', false,
  977. array('\home\images',
  978. '\home\uploads')
  979. );
  980. ]]></programlisting>
  981. </example>
  982. <note>
  983. <para>
  984. Note that this validator checks if the file does not exist in all of the provided
  985. directories. The validation will fail if the file does exist in any of the given
  986. directories.
  987. </para>
  988. </note>
  989. </sect2>
  990. <sect2 id="zend.file.transfer.validators.sha1">
  991. <title>Sha1 Validator</title>
  992. <para>
  993. The <code>Sha1</code> validator checks the content of a transferred file by hashing it.
  994. This validator uses the hash extension for PHP with the sha1 algorithm.
  995. It supports the following options:
  996. </para>
  997. <itemizedlist>
  998. <listitem>
  999. <para>
  1000. <code>*</code>: Takes any key or use a numeric array.
  1001. </para>
  1002. <para>
  1003. You can set multiple hashes by passing them as an array. Each file is checked,
  1004. and the validation will fail only if all files fail validation.
  1005. </para>
  1006. </listitem>
  1007. </itemizedlist>
  1008. <example id="zend.file.transfer.validators.sha1.example">
  1009. <title>Using the sha1 Validator</title>
  1010. <programlisting language="php"><![CDATA[
  1011. $upload = new Zend_File_Transfer();
  1012. // Checks if the content of the uploaded file has the given hash
  1013. $upload->addValidator('sha1', false, '3b3652f336522365223');
  1014. // Limits this validator to two different hashes
  1015. $upload->addValidator('Sha1',
  1016. false, array('3b3652f336522365223',
  1017. 'eb3365f3365ddc65365'));
  1018. ]]></programlisting>
  1019. </example>
  1020. </sect2>
  1021. <sect2 id="zend.file.transfer.validators.size">
  1022. <title>Size Validator</title>
  1023. <para>
  1024. The <code>Size</code> validator checks for the size of a single file. It
  1025. supports the following options:
  1026. </para>
  1027. <itemizedlist>
  1028. <listitem>
  1029. <para>
  1030. <code>min</code>: Sets the minimum file size.
  1031. </para>
  1032. </listitem>
  1033. <listitem>
  1034. <para>
  1035. <code>max</code>: Sets the maximum file size.
  1036. </para>
  1037. </listitem>
  1038. <listitem>
  1039. <para>
  1040. <code>bytestring</code>: Defines whether a failure is returned with a
  1041. user-friendly number, or with the plain file size.
  1042. </para>
  1043. <para>
  1044. With this option you can define if the user gets '10864' or '10MB'. Default
  1045. value is true which returns '10MB'.
  1046. </para>
  1047. </listitem>
  1048. </itemizedlist>
  1049. <para>
  1050. You can initialize this validator with a string, which will then be used to set the
  1051. <code>max</code> option. You can also use the methods <code>setMin()</code> and
  1052. <code>setMax()</code> to set both options after construction, along with
  1053. <code>getMin()</code> and <code>getMax()</code> to retrieve the values that have been
  1054. set previously.
  1055. </para>
  1056. <para>
  1057. The size itself is also accepted in SI notation as handled by most operating systems.
  1058. That is, instead of specifying <emphasis>20000 bytes</emphasis>,
  1059. <emphasis>20kB</emphasis> may be given. All file sizes are converted using 1024 as the
  1060. base value. The following Units are accepted: <code>kB</code>, <code>MB</code>,
  1061. <code>GB</code>, <code>TB</code>, <code>PB</code> and <code>EB</code>. Note that 1kB is
  1062. equal to 1024 bytes, 1MB is equal to 1024kB, and so on.
  1063. </para>
  1064. <example id="zend.file.transfer.validators.size.example">
  1065. <title>Using the Size Validator</title>
  1066. <programlisting language="php"><![CDATA[
  1067. $upload = new Zend_File_Transfer();
  1068. // Limit the size of a file to 40000 bytes
  1069. $upload->addValidator('Size', false, 40000);
  1070. // Limit the size a given file to maximum 4MB and mimimum 10kB
  1071. // Also returns the plain number in case of an error
  1072. // instead of a user-friendly number
  1073. $upload->addValidator('Size',
  1074. false,
  1075. array('min' => '10kB',
  1076. 'max' => '4MB',
  1077. 'bytestring' => false));
  1078. ]]></programlisting>
  1079. </example>
  1080. </sect2>
  1081. <sect2 id="zend.file.transfer.validators.wordcount">
  1082. <title>WordCount Validator</title>
  1083. <para>
  1084. The <code>WordCount</code> validator checks for the number of words within provided
  1085. files. It supports the following option keys:
  1086. </para>
  1087. <itemizedlist>
  1088. <listitem>
  1089. <para>
  1090. <code>min</code>: Sets the minimum number of words to be found.
  1091. </para>
  1092. </listitem>
  1093. <listitem>
  1094. <para>
  1095. <code>max</code>: Sets the maximum number of words to be found.
  1096. </para>
  1097. </listitem>
  1098. </itemizedlist>
  1099. <para>
  1100. If you initiate this validator with a string or integer, the value will be used as
  1101. <code>max</code>. Or you can also use the methods <code>setMin()</code> and
  1102. <code>setMax()</code> to set both options afterwards and <code>getMin()</code> and
  1103. <code>getMax()</code> to retrieve the actual set values.
  1104. </para>
  1105. <example id="zend.file.transfer.validators.wordcount.example">
  1106. <title>Using the WordCount Validator</title>
  1107. <programlisting language="php"><![CDATA[
  1108. $upload = new Zend_File_Transfer();
  1109. // Limit the amount of words within files to maximum 2000
  1110. $upload->addValidator('WordCount', false, 2000);
  1111. // Limit the amount of words within files to maximum 5000 and minimum 1000 words
  1112. $upload->addValidator('WordCount', false, array('min' => 1000, 'max' => 5000));
  1113. ]]></programlisting>
  1114. </example>
  1115. </sect2>
  1116. </sect1>
  1117. <!--
  1118. vim:se ts=4 sw=4 tw=80 et:
  1119. -->