Zend_File_Transfer-Validators.xml 50 KB

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