1 <?xml version="1.0" encoding="UTF-8"?>
3 <sect1 id="zend.file.transfer.validators">
4 <title>Validators for Zend_File_Transfer</title>
7 <classname>Zend_File_Transfer</classname> is delivered with several file-related validators
8 which can be used to increase security and prevent possible attacks. Note that these
9 validators are only as effective as how effectively you apply them. All validators provided
10 with <classname>Zend_File_Transfer</classname> can be found in the
11 <classname>Zend_Validator</classname> component and are named
12 <classname>Zend_Validate_File_*</classname>. The following validators are available:
18 <classname>Count</classname>: This validator checks for the number of files. A
19 minimum and maximum range can be specified. An error will be thrown if either limit
26 <classname>Crc32</classname>: This validator checks for the crc32 hash value of the
27 content from a file. It is based on the <classname>Hash</classname> validator and
28 provides a convenient and simple validator that only supports Crc32.
34 <classname>ExcludeExtension</classname>: This validator checks the extension of
35 files. It will throw an error when an given file has a defined extension. With this
36 validator, you can exclude defined extensions from being validated.
42 <classname>ExcludeMimeType</classname>: This validator validates the
43 <acronym>MIME</acronym> type of files. It can also validate <acronym>MIME</acronym>
44 types and will throw an error if the <acronym>MIME</acronym> type of specified file
51 <classname>Exists</classname>: This validator checks for the existence of files. It
52 will throw an error when a specified file does not exist.
58 <classname>Extension</classname>: This validator checks the extension of files. It
59 will throw an error when a specified file has an undefined extension.
65 <classname>FilesSize</classname>: This validator checks the size of validated files.
66 It remembers internally the size of all checked files and throws an error when the
67 sum of all specified files exceed the defined size. It also provides minimum and
74 <classname>ImageSize</classname>: This validator checks the size of image. It
75 validates the width and height and enforces minimum and maximum dimensions.
81 <classname>IsCompressed</classname>: This validator checks whether the file is
82 compressed. It is based on the <classname>MimeType</classname> validator and
83 validates for compression archives like zip or arc. You can also limit it to other
90 <classname>IsImage</classname>: This validator checks whether the file is an image.
91 It is based on the <classname>MimeType</classname> validator and validates for image
92 files like jpg or gif. You can also limit it to other image types.
98 <classname>Hash</classname>: This validator checks the hash value of the content
99 from a file. It supports multiple algorithms.
105 <classname>Md5</classname>: This validator checks for the md5 hash value of the
106 content from a file. It is based on the <classname>Hash</classname> validator and
107 provides a convenient and simple validator that only supports Md5.
113 <classname>MimeType</classname>: This validator validates the
114 <acronym>MIME</acronym> type of files. It can also validate <acronym>MIME</acronym>
115 types and will throw an error if the <acronym>MIME</acronym> type of a specified
122 <classname>NotExists</classname>: This validator checks for the existence of files.
123 It will throw an error when an given file does exist.
129 <classname>Sha1</classname>: This validator checks for the sha1 hash value of the
130 content from a file. It is based on the <classname>Hash</classname> validator and
131 provides a convenient and simple validator that only supports sha1.
137 <classname>Size</classname>: This validator is able to check files for its file
138 size. It provides a minimum and maximum size range and will throw an error when
139 either of these thesholds are crossed.
145 <classname>Upload</classname>: This validator is internal. It checks if an upload
146 has resulted in an error. You must not set it, as it's automatically set by
147 <classname>Zend_File_Transfer</classname> itself. So you do not use this validator
148 directly. You should only know that it exists.
154 <classname>WordCount</classname>: This validator is able to check the number of
155 words within files. It provides a minimum and maximum count and will throw an error
156 when either of these thresholds are crossed.
161 <sect2 id="zend.file.transfer.validators.usage">
162 <title>Using Validators with Zend_File_Transfer</title>
165 Putting validators to work is quite simple. There are several methods for adding and
166 manipulating validators:
172 <methodname>isValid($files = null)</methodname>: Checks the specified files
173 using all validators. <varname>$files</varname> may be either a real filename,
174 the element's name or the name of the temporary file.
180 <methodname>addValidator($validator, $breakChainOnFailure, $options = null,
181 $files = null)</methodname>: Adds the specified validator to the validator
182 stack (optionally only to the file(s) specified).
183 <varname>$validator</varname> may be either an actual validator
184 instance or a short name specifying the validator type (e.g., 'Count').
190 <methodname>addValidators(array $validators, $files =
191 null)</methodname>: Adds the specified validators to the stack of
192 validators. Each entry may be either a validator
193 type/options pair or an array with the key 'validator'
194 specifying the validator. All other options will be
195 considered validator options for instantiation.
201 <methodname>setValidators(array $validators, $files =
202 null)</methodname>: Overwrites any existing validators with
203 the validators specified. The validators should follow the
204 syntax for <methodname>addValidators()</methodname>.
210 <methodname>hasValidator($name)</methodname>: Indicates whether a
211 validator has been registered.
217 <methodname>getValidator($name)</methodname>: Returns a previously
218 registered validator.
224 <methodname>getValidators($files = null)</methodname>: Returns
225 registered validators. If <varname>$files</varname> is specified,
226 returns validators for that particular file or set of
233 <methodname>removeValidator($name)</methodname>: Removes a previously
234 registered validator.
240 <methodname>clearValidators()</methodname>: Clears all
241 registered validators.
246 <example id="zend.file.transfer.validators.usage.example">
247 <title>Add Validators to a File Transfer Object</title>
249 <programlisting language="php"><![CDATA[
250 $upload = new Zend_File_Transfer();
252 // Set a file size with 20000 bytes
253 $upload->addValidator('Size', false, 20000);
255 // Set a file size with 20 bytes minimum and 20000 bytes maximum
256 $upload->addValidator('Size', false, array('min' => 20, 'max' => 20000));
258 // Set a file size with 20 bytes minimum and 20000 bytes maximum and
259 // a file count in one step
260 $upload->setValidators(array(
261 'Size' => array('min' => 20, 'max' => 20000),
262 'Count' => array('min' => 1, 'max' => 3),
267 <example id="zend.file.transfer.validators.usage.exampletwo">
268 <title>Limit Validators to Single Files</title>
271 <methodname>addValidator()</methodname>, <methodname>addValidators()</methodname>,
272 and <methodname>setValidators()</methodname> each accept a final
273 <varname>$files</varname> argument. This argument can be used to
274 specify a particular file or array of files on which to set the
278 <programlisting language="php"><![CDATA[
279 $upload = new Zend_File_Transfer();
281 // Set a file size with 20000 bytes and limits it only to 'file2'
282 $upload->addValidator('Size', false, 20000, 'file2');
287 Normally, you should use the <methodname>addValidators()</methodname> method, which
288 can be called multiple times.
291 <example id="zend.file.transfer.validators.usage.examplemultiple">
292 <title>Add Multiple Validators</title>
295 Often it's simpler just to call <methodname>addValidator()</methodname> multiple
296 times with one call for each validator. This also increases readability and makes
297 your code more maintainable. All methods provide a fluent interface, so you can
298 couple the calls as shown below:
301 <programlisting language="php"><![CDATA[
302 $upload = new Zend_File_Transfer();
304 // Set a file size with 20000 bytes
305 $upload->addValidator('Size', false, 20000)
306 ->addValidator('Count', false, 2)
307 ->addValidator('Filessize', false, 25000);
313 Note that setting the same validator
314 multiple times is allowed, but doing so can lead to issues when using
315 different options for the same validator.
320 Last but not least, you can simply check the files using
321 <methodname>isValid()</methodname>.
324 <example id="zend.file.transfer.validators.usage.exampleisvalid">
325 <title>Validate the Files</title>
328 <methodname>isValid()</methodname> accepts the file name of the uploaded or
329 downloaded file, the temporary file name and or the name of the form element. If
330 no parameter or null is given all files will be validated
333 <programlisting language="php"><![CDATA[
334 $upload = new Zend_File_Transfer();
336 // Set a file size with 20000 bytes
337 $upload->addValidator('Size', false, 20000)
338 ->addValidator('Count', false, 2)
339 ->addValidator('Filessize', false, 25000);
341 if ($upload->isValid()) {
342 print "Validation failure";
349 Note that <methodname>isValid()</methodname> will be called automatically when you
350 receive the files and have not called it previously.
355 When validation has failed it is a good idea to get information about the
356 problems found. To get this information, you can use the methods
357 <methodname>getMessages()</methodname> which returns all validation messages as array,
358 <methodname>getErrors()</methodname> which returns all error codes, and
359 <methodname>hasErrors()</methodname> which returns <constant>TRUE</constant> as soon as
360 a validation error has been found.
364 <sect2 id="zend.file.transfer.validators.count">
365 <title>Count Validator</title>
368 The <classname>Count</classname> validator checks for the number of files which are
369 provided. It supports the following option keys:
375 <property>min</property>: Sets the minimum number of files to transfer.
380 When using this option you must give the minimum number of files when
381 calling this validator the first time; otherwise you will get an error in
387 With this option you can define the minimum number of files you expect to
394 <property>max</property>: Sets the maximum number of files to transfer.
398 With this option you can limit the number of files which are accepted but also
399 detect a possible attack when more files are given than defined in your form.
405 If you initiate this validator with a string or integer, the value will be used as
406 <property>max</property>. Or you can also use the methods
407 <methodname>setMin()</methodname> and <methodname>setMax()</methodname> to set both
408 options afterwards and <methodname>getMin()</methodname> and
409 <methodname>getMax()</methodname> to retrieve the actual set values.
412 <example id="zend.file.transfer.validators.count.example">
413 <title>Using the Count Validator</title>
415 <programlisting language="php"><![CDATA[
416 $upload = new Zend_File_Transfer();
418 // Limit the amount of files to maximum 2
419 $upload->addValidator('Count', false, 2);
421 // Limit the amount of files to maximum 5 and minimum 1 file
422 $upload->addValidator('Count', false, array('min' =>1, 'max' => 5));
428 Note that this validator stores the number of checked files internally. The file
429 which exceeds the maximum will be returned as error.
434 <sect2 id="zend.file.transfer.validators.crc32">
435 <title>Crc32 Validator</title>
438 The <classname>Crc32</classname> validator checks the content of a transferred file by
439 hashing it. This validator uses the hash extension from <acronym>PHP</acronym> with the
440 crc32 algorithm. It supports the following options:
446 <property>*</property>: Sets any key or use a numeric array. The values will be
447 used as hash to validate against.
451 You can set multiple hashes by using different keys. Each will be checked and
452 the validation will fail only if all values fail.
457 <example id="zend.file.transfer.validators.crc32.example">
458 <title>Using the Crc32 Validator</title>
460 <programlisting language="php"><![CDATA[
461 $upload = new Zend_File_Transfer();
463 // Checks whether the content of the uploaded file has the given hash
464 $upload->addValidator('Crc32', false, '3b3652f');
466 // Limits this validator to two different hashes
467 $upload->addValidator('Crc32', false, array('3b3652f', 'e612b69'));
472 <sect2 id="zend.file.transfer.validators.excludeextension">
473 <title>ExcludeExtension Validator</title>
476 The <classname>ExcludeExtension</classname> validator checks the file extension of the
477 specified files. It supports the following options:
483 <property>*</property>: Sets any key or use a numeric array. The values will be
484 used to check whether the given file does not use this file extension.
490 <property>case</property>: Sets a boolean indicating whether validation should
491 be case-sensitive. The default is not case sensitive. Note that this key can be
492 applied to for all available extensions.
498 This validator accepts multiple extensions, either as a comma-delimited string, or as
499 an array. You may also use the methods <methodname>setExtension()</methodname>,
500 <methodname>addExtension()</methodname>, and <methodname>getExtension()</methodname>
501 to set and retrieve extensions.
505 In some cases it is useful to match in a case-sensitive fashion. So the constructor
506 allows a second parameter called <varname>$case</varname> which, if set to
507 <constant>TRUE</constant>, validates the extension by comparing it with the specified
508 values in a case-sensitive fashion.
511 <example id="zend.file.transfer.validators.excludeextension.example">
512 <title>Using the ExcludeExtension Validator</title>
514 <programlisting language="php"><![CDATA[
515 $upload = new Zend_File_Transfer();
517 // Do not allow files with extension php or exe
518 $upload->addValidator('ExcludeExtension', false, 'php,exe');
520 // Do not allow files with extension php or exe, but use array notation
521 $upload->addValidator('ExcludeExtension', false, array('php', 'exe'));
523 // Check in a case-sensitive fashion
524 $upload->addValidator('ExcludeExtension',
526 array('php', 'exe', 'case' => true));
527 $upload->addValidator('ExcludeExtension',
529 array('php', 'exe', 'case' => true));
535 Note that this validator only checks the file extension. It does not check the
536 file's <acronym>MIME</acronym> type.
541 <sect2 id="zend.file.transfer.validators.excludemimetype">
542 <title>ExcludeMimeType Validator</title>
545 The <classname>ExcludeMimeType</classname> validator checks the <acronym>MIME</acronym>
546 type of transferred files. It supports the following options:
552 <property>*</property>: Sets any key individually or use a numeric array. Sets
553 the <acronym>MIME</acronym> type to validate against.
557 With this option you can define the <acronym>MIME</acronym> type of files that
558 are not to be accepted.
564 <property>headerCheck</property>: If set to <constant>TRUE</constant> this
565 option will check the <acronym>HTTP</acronym> Information for the file type when
566 the <emphasis>fileInfo</emphasis> or <emphasis>mimeMagic</emphasis> extensions
567 can not be found. The default value for this option is
568 <constant>FALSE</constant>.
574 This validator accepts multiple <acronym>MIME</acronym> types, either as a
575 comma-delimited string, or as an array. You may also use the methods
576 <methodname>setMimeType()</methodname>, <methodname>addMimeType()</methodname>, and
577 <methodname>getMimeType()</methodname> to set and retrieve the <acronym>MIME</acronym>
581 <example id="zend.file.transfer.validators.excludemimetype.example">
582 <title>Using the ExcludeMimeType Validator</title>
584 <programlisting language="php"><![CDATA[
585 $upload = new Zend_File_Transfer();
587 // Does not allow MIME type of gif images for all files
588 $upload->addValidator('ExcludeMimeType', false, 'image/gif');
590 // Does not allow MIME type of gif and jpg images for all given files
591 $upload->addValidator('ExcludeMimeType', false, array('image/gif',
594 // Does not allow MIME type of the group images for all given files
595 $upload->addValidator('ExcludeMimeType', false, 'image');
600 The above example shows that it is also possible to disallow groups of
601 <acronym>MIME</acronym> types. For example, to disallow all images, just use 'image' as
602 the <acronym>MIME</acronym> type. This can be used for all groups of
603 <acronym>MIME</acronym> types like 'image', 'audio', 'video', 'text', etc.
608 Note that disallowing groups of <acronym>MIME</acronym> types will disallow all
609 members of this group even if this is not intentional. When you disallow 'image' you
610 will disallow all types of images like 'image/jpeg' or 'image/vasa'. When you are
611 not sure if you want to disallow all types, you should disallow only specific
612 <acronym>MIME</acronym> types instead of complete groups.
617 <sect2 id="zend.file.transfer.validators.exists">
618 <title>Exists Validator</title>
621 The <classname>Exists</classname> validator checks for the existence of specified
622 files. It supports the following options:
628 <property>*</property>: Sets any key or use a numeric array to check if the
629 specific file exists in the given directory.
635 This validator accepts multiple directories, either as a comma-delimited string, or as
636 an array. You may also use the methods <methodname>setDirectory()</methodname>,
637 <methodname>addDirectory()</methodname>, and <methodname>getDirectory()</methodname>
638 to set and retrieve directories.
641 <example id="zend.file.transfer.validators.exists.example">
642 <title>Using the Exists Validator</title>
644 <programlisting language="php"><![CDATA[
645 $upload = new Zend_File_Transfer();
647 // Add the temp directory to check for
648 $upload->addValidator('Exists', false, '\temp');
650 // Add two directories using the array notation
651 $upload->addValidator('Exists',
653 array('\home\images', '\home\uploads'));
659 Note that this validator checks whether the specified file exists in all of the
660 given directories. The validation will fail if the file does not exist in any of the
666 <sect2 id="zend.file.transfer.validators.extension">
667 <title>Extension Validator</title>
670 The <classname>Extension</classname> validator checks the file extension of the
671 specified files. It supports the following options:
677 <property>*</property>: Sets any key or use a numeric array to check whether the
678 specified file has this file extension.
684 <property>case</property>: Sets whether validation should be done in a
685 case-sensitive fashion. The default is no case sensitivity. Note the this key is
686 used for all given extensions.
692 This validator accepts multiple extensions, either as a comma-delimited string, or as an
693 array. You may also use the methods <methodname>setExtension()</methodname>,
694 <methodname>addExtension()</methodname>, and <methodname>getExtension()</methodname> to
695 set and retrieve extension values.
699 In some cases it is useful to test in a case-sensitive fashion. Therefore the
700 constructor takes a second parameter <varname>$case</varname>, which, if set to
701 <constant>TRUE</constant>, will validate the extension in a case-sensitive fashion.
704 <example id="zend.file.transfer.validators.extension.example">
705 <title>Using the Extension Validator</title>
707 <programlisting language="php"><![CDATA[
708 $upload = new Zend_File_Transfer();
710 // Limit the extensions to jpg and png files
711 $upload->addValidator('Extension', false, 'jpg,png');
713 // Limit the extensions to jpg and png files but use array notation
714 $upload->addValidator('Extension', false, array('jpg', 'png'));
716 // Check case sensitive
717 $upload->addValidator('Extension', false, array('mo', 'png', 'case' => true));
718 if (!$upload->isValid('C:\temp\myfile.MO')) {
719 print 'Not valid because MO and mo do not match with case sensitivity;
726 Note that this validator only checks the file extension. It does not check the
727 file's <acronym>MIME</acronym> type.
732 <sect2 id="zend.file.transfer.validators.filessize">
733 <title>FilesSize Validator</title>
736 The <classname>FilesSize</classname> validator checks for the aggregate size of all
737 transferred files. It supports the following options:
743 <property>min</property>: Sets the minimum aggregate file size.
744 This option defines the minimum aggregate file size to be transferred.
750 <property>max</property>: Sets the maximum aggregate file size.
754 This option limits the aggregate file size of all transferred files, but not the
755 file size of individual files.
761 <property>bytestring</property>: Defines whether a failure is to return a
762 user-friendly number or the plain file size.
766 This option defines whether the user sees '10864' or '10MB'. The default value
767 is <constant>TRUE</constant>, so '10MB' is returned if you did not specify
774 You can initialize this validator with a string, which will then be used to set the
775 <property>max</property> option. You can also use the methods
776 <methodname>setMin()</methodname> and <methodname>setMax()</methodname> to set both
777 options after construction, along with <methodname>getMin()</methodname> and
778 <methodname>getMax()</methodname> to retrieve the values that have been set previously.
782 The size itself is also accepted in SI notation as handled by most operating systems.
783 That is, instead of specifying <emphasis>20000 bytes</emphasis>,
784 <emphasis>20kB</emphasis> may be given. All file sizes are converted using 1024 as the
785 base value. The following Units are accepted: <emphasis>kB</emphasis>,
786 <emphasis>MB</emphasis>, <emphasis>GB</emphasis>, <emphasis>TB</emphasis>,
787 <emphasis>PB</emphasis> and <emphasis>EB</emphasis>. Note that 1kB is equal to 1024
788 bytes, 1MB is equal to 1024kB, and so on.
791 <example id="zend.file.transfer.validators.filessize.example">
792 <title>Using the FilesSize Validator</title>
794 <programlisting language="php"><![CDATA[
795 $upload = new Zend_File_Transfer();
797 // Limit the size of all files to be uploaded to 40000 bytes
798 $upload->addValidator('FilesSize', false, 40000);
800 // Limit the size of all files to be uploaded to maximum 4MB and mimimum 10kB
801 $upload->addValidator('FilesSize',
803 array('min' => '10kB', 'max' => '4MB'));
805 // As before, but returns the plain file size instead of a user-friendly string
806 $upload->addValidator('FilesSize',
808 array('min' => '10kB',
810 'bytestring' => false));
816 Note that this validator internally stores the file size of checked files. The file
817 which exceeds the size will be returned as an error.
822 <sect2 id="zend.file.transfer.validators.imagesize">
823 <title>ImageSize Validator</title>
826 The <classname>ImageSize</classname> validator checks the size of image files.
827 It supports the following options:
833 <property>minheight</property>: Sets the minimum image height.
839 <property>maxheight</property>: Sets the maximum image height.
845 <property>minwidth</property>: Sets the minimum image width.
851 <property>maxwidth</property>: Sets the maximum image width.
857 The methods <methodname>setImageMin()</methodname> and
858 <methodname>setImageMax()</methodname> also set both minimum and maximum values, while
859 the methods <methodname>getMin()</methodname> and <methodname>getMax()</methodname>
860 return the currently set values.
864 For your convenience there are also the <methodname>setImageWidth()</methodname> and
865 <methodname>setImageHeight()</methodname> methods, which set the minimum and maximum
866 height and width of the image file. They, too, have corresponding
867 <methodname>getImageWidth()</methodname> and <methodname>getImageHeight()</methodname>
868 methods to retrieve the currently set values.
872 To bypass validation of a particular dimension, the relevent option simply should not be
876 <example id="zend.file.transfer.validators.imagesize.example">
877 <title>Using the ImageSize Validator</title>
879 <programlisting language="php"><![CDATA[
880 $upload = new Zend_File_Transfer();
882 // Limit the size of a image to a height of 100-200 and a width of
884 $upload->addValidator('ImageSize', false,
885 array('minwidth' => 40,
891 // Reset the width for validation
892 $upload->setImageWidth(array('minwidth' => 20, 'maxwidth' => 200));
897 <sect2 id="zend.file.transfer.validators.iscompressed">
898 <title>IsCompressed Validator</title>
901 The <classname>IsCompressed</classname> validator checks if a transferred file is a
902 compressed archive, such as zip or arc. This validator is based on the
903 <classname>MimeType</classname> validator and supports the same methods and options.
904 You may also limit this validator to particular compression types with the methods
908 <example id="zend.file.transfer.validators.iscompressed.example">
909 <title>Using the IsCompressed Validator</title>
911 <programlisting language="php"><![CDATA[
912 $upload = new Zend_File_Transfer();
914 // Checks is the uploaded file is a compressed archive
915 $upload->addValidator('IsCompressed', false);
917 // Limits this validator to zip files only
918 $upload->addValidator('IsCompressed', false, array('application/zip'));
920 // Limits this validator to zip files only using simpler notation
921 $upload->addValidator('IsCompressed', false, 'zip');
927 Note that there is no check if you set a <acronym>MIME</acronym> type that is not a
928 archive. For example, it would be possible to define gif files to be accepted by
929 this validator. Using the 'MimeType' validator for files which are not archived will
930 result in more readable code.
935 <sect2 id="zend.file.transfer.validators.isimage">
936 <title>IsImage Validator</title>
939 The <classname>IsImage</classname> validator checks if a transferred file is a image
940 file, such as gif or jpeg. This validator is based on the
941 <classname>MimeType</classname> validator and supports the same methods and options.
942 You can limit this validator to particular image types with the methods described there.
945 <example id="zend.file.transfer.validators.isimage.example">
946 <title>Using the IsImage Validator</title>
948 <programlisting language="php"><![CDATA[
949 $upload = new Zend_File_Transfer();
951 // Checks whether the uploaded file is a image file
952 $upload->addValidator('IsImage', false);
954 // Limits this validator to gif files only
955 $upload->addValidator('IsImage', false, array('application/gif'));
957 // Limits this validator to jpeg files only using a simpler notation
958 $upload->addValidator('IsImage', false, 'jpeg');
964 Note that there is no check if you set a <acronym>MIME</acronym> type that is not an
965 image. For example, it would be possible to define zip files to be accepted by this
966 validator. Using the 'MimeType' validator for files which are not images will result
967 in more readable code.
972 <sect2 id="zend.file.transfer.validators.hash">
973 <title>Hash Validator</title>
976 The <classname>Hash</classname> validator checks the content of a transferred file by
977 hashing it. This validator uses the hash extension from <acronym>PHP</acronym>. It
978 supports the following options:
984 <property>*</property>: Takes any key or use a numeric array. Sets the hash to
989 You can set multiple hashes by passing them as an array. Each file is checked,
990 and the validation will fail only if all files fail validation.
996 <property>algorithm</property>: Sets the algorithm to use for hashing the
1001 You can set multiple algorithm by calling the <methodname>addHash()</methodname>
1002 method multiple times.
1007 <example id="zend.file.transfer.validators.hash.example">
1008 <title>Using the Hash Validator</title>
1010 <programlisting language="php"><![CDATA[
1011 $upload = new Zend_File_Transfer();
1013 // Checks if the content of the uploaded file contains the given hash
1014 $upload->addValidator('Hash', false, '3b3652f');
1016 // Limits this validator to two different hashes
1017 $upload->addValidator('Hash', false, array('3b3652f', 'e612b69'));
1019 // Sets a different algorithm to check against
1020 $upload->addValidator('Hash',
1022 array('315b3cd8273d44912a7',
1023 'algorithm' => 'md5'));
1024 ]]></programlisting>
1029 This validator supports about 34 different hash algorithms. The most common include
1030 'crc32', 'md5' and 'sha1'. A comprehesive list of supports hash algorithms can be
1031 found at the <ulink url="http://php.net/hash_algos">hash_algos method</ulink> on the
1032 <ulink url="http://php.net">php.net site</ulink>.
1037 <sect2 id="zend.file.transfer.validators.md5">
1038 <title>Md5 Validator</title>
1041 The <classname>Md5</classname> validator checks the content of a transferred file by
1042 hashing it. This validator uses the hash extension for <acronym>PHP</acronym> with the
1043 md5 algorithm. It supports the following options:
1049 <property>*</property>: Takes any key or use a numeric array.
1053 You can set multiple hashes by passing them as an array. Each file is checked,
1054 and the validation will fail only if all files fail validation.
1059 <example id="zend.file.transfer.validators.md5.example">
1060 <title>Using the Md5 Validator</title>
1062 <programlisting language="php"><![CDATA[
1063 $upload = new Zend_File_Transfer();
1065 // Checks if the content of the uploaded file has the given hash
1066 $upload->addValidator('Md5', false, '3b3652f336522365223');
1068 // Limits this validator to two different hashes
1069 $upload->addValidator('Md5',
1071 array('3b3652f336522365223',
1072 'eb3365f3365ddc65365'));
1073 ]]></programlisting>
1077 <sect2 id="zend.file.transfer.validators.mimetype">
1078 <title>MimeType Validator</title>
1081 The <classname>MimeType</classname> validator checks the <acronym>MIME</acronym> type of
1082 transferred files. It supports the following options:
1088 <property>*</property>: Sets any key or use a numeric array. Sets the
1089 <acronym>MIME</acronym> type to validate against.
1093 Defines the <acronym>MIME</acronym> type of files to be accepted.
1099 <property>headerCheck</property>: If set to <constant>TRUE</constant> this
1100 option will check the <acronym>HTTP</acronym> Information for the file type when
1101 the <emphasis>fileInfo</emphasis> or <emphasis>mimeMagic</emphasis> extensions
1102 can not be found. The default value for this option is
1103 <constant>FALSE</constant>.
1109 <property>magicfile</property>: The magicfile to be used.
1113 With this option you can define which magicfile to use. When it's not set or
1114 empty, the MAGIC constant will be used instead. This option is available since
1115 Zend Framework 1.7.1.
1121 This validator accepts multiple <acronym>MIME</acronym> type, either as a
1122 comma-delimited string, or as an array. You may also use the methods
1123 <methodname>setMimeType()</methodname>, <methodname>addMimeType()</methodname>, and
1124 <methodname>getMimeType()</methodname> to set and retrieve <acronym>MIME</acronym>
1129 You can also set the magicfile which shall be used by fileinfo with the 'magicfile'
1130 option. Additionally there are convenient <methodname>setMagicFile()</methodname> and
1131 <methodname>getMagicFile()</methodname> methods which allow later setting and retrieving
1132 of the magicfile parameter. This methods are available since Zend Framework 1.7.1.
1135 <example id="zend.file.transfer.validators.mimetype.example">
1136 <title>Using the MimeType Validator</title>
1138 <programlisting language="php"><![CDATA[
1139 $upload = new Zend_File_Transfer();
1141 // Limit the MIME type of all given files to gif images
1142 $upload->addValidator('MimeType', false, 'image/gif');
1144 // Limit the MIME type of all given files to gif and jpeg images
1145 $upload->addValidator('MimeType', false, array('image/gif', 'image/jpeg');
1147 // Limit the MIME type of all given files to the group images
1148 $upload->addValidator('MimeType', false, 'image');
1150 // Use a different magicfile
1151 $upload->addValidator('MimeType',
1154 'magicfile' => '/path/to/magicfile.mgx'));
1155 ]]></programlisting>
1159 The above example shows that it is also possible to limit the accepted
1160 <acronym>MIME</acronym> type to a group of <acronym>MIME</acronym> types. To allow all
1161 images just use 'image' as <acronym>MIME</acronym> type. This can be used for all groups
1162 of <acronym>MIME</acronym> types like 'image', 'audio', 'video', 'text, and so on.
1167 Note that allowing groups of <acronym>MIME</acronym> types will accept all members
1168 of this group even if your application does not support them. When you allow 'image'
1169 you will also get 'image/xpixmap' or 'image/vasa' which could be problematic. When
1170 you are not sure if your application supports all types you should better allow only
1171 defined <acronym>MIME</acronym> types instead of the complete group.
1177 This component will use the <classname>FileInfo</classname> extension if it is
1178 available. If it's not, it will degrade to the
1179 <methodname>mime_content_type()</methodname> function. And if the function call
1180 fails it will use the <acronym>MIME</acronym> type which is given by
1181 <acronym>HTTP</acronym>.
1185 You should be aware of possible security problems when you have whether
1186 <classname>FileInfo</classname> nor <methodname>mime_content_type()</methodname>
1187 available. The <acronym>MIME</acronym> type given by <acronym>HTTP</acronym> is not
1188 secure and can be easily manipulated.
1193 <sect2 id="zend.file.transfer.validators.notexists">
1194 <title>NotExists Validator</title>
1197 The <classname>NotExists</classname> validator checks for the existence of the provided
1198 files. It supports the following options:
1204 <property>*</property>: Set any key or use a numeric array. Checks whether the
1205 file exists in the given directory.
1211 This validator accepts multiple directories either as a comma-delimited string, or as an
1212 array. You may also use the methods <methodname>setDirectory()</methodname>,
1213 <methodname>addDirectory()</methodname>, and <methodname>getDirectory()</methodname> to
1214 set and retrieve directories.
1217 <example id="zend.file.transfer.validators.notexists.example">
1218 <title>Using the NotExists Validator</title>
1220 <programlisting language="php"><![CDATA[
1221 $upload = new Zend_File_Transfer();
1223 // Add the temp directory to check
1224 $upload->addValidator('NotExists', false, '\temp');
1226 // Add two directories using the array notation
1227 $upload->addValidator('NotExists', false,
1228 array('\home\images',
1231 ]]></programlisting>
1236 Note that this validator checks if the file does not exist in all of the provided
1237 directories. The validation will fail if the file does exist in any of the given
1243 <sect2 id="zend.file.transfer.validators.sha1">
1244 <title>Sha1 Validator</title>
1247 The <classname>Sha1</classname> validator checks the content of a transferred file by
1248 hashing it. This validator uses the hash extension for <acronym>PHP</acronym> with the
1249 sha1 algorithm. It supports the following options:
1255 <property>*</property>: Takes any key or use a numeric array.
1259 You can set multiple hashes by passing them as an array. Each file is checked,
1260 and the validation will fail only if all files fail validation.
1265 <example id="zend.file.transfer.validators.sha1.example">
1266 <title>Using the sha1 Validator</title>
1268 <programlisting language="php"><![CDATA[
1269 $upload = new Zend_File_Transfer();
1271 // Checks if the content of the uploaded file has the given hash
1272 $upload->addValidator('sha1', false, '3b3652f336522365223');
1274 // Limits this validator to two different hashes
1275 $upload->addValidator('Sha1',
1276 false, array('3b3652f336522365223',
1277 'eb3365f3365ddc65365'));
1278 ]]></programlisting>
1282 <sect2 id="zend.file.transfer.validators.size">
1283 <title>Size Validator</title>
1286 The <classname>Size</classname> validator checks for the size of a single file. It
1287 supports the following options:
1293 <property>min</property>: Sets the minimum file size.
1299 <property>max</property>: Sets the maximum file size.
1305 <property>bytestring</property>: Defines whether a failure is returned with a
1306 user-friendly number, or with the plain file size.
1310 With this option you can define if the user gets '10864' or '10MB'. Default
1311 value is <constant>TRUE</constant> which returns '10MB'.
1317 You can initialize this validator with a string, which will then be used to set the
1318 <property>max</property> option. You can also use the methods
1319 <methodname>setMin()</methodname> and <methodname>setMax()</methodname> to set both
1320 options after construction, along with <methodname>getMin()</methodname> and
1321 <methodname>getMax()</methodname> to retrieve the values that have been set previously.
1325 The size itself is also accepted in SI notation as handled by most operating systems.
1326 That is, instead of specifying <emphasis>20000 bytes</emphasis>,
1327 <emphasis>20kB</emphasis> may be given. All file sizes are converted using 1024 as the
1328 base value. The following Units are accepted: <emphasis>kB</emphasis>,
1329 <emphasis>MB</emphasis>, <emphasis>GB</emphasis>, <emphasis>TB</emphasis>,
1330 <emphasis>PB</emphasis> and <emphasis>EB</emphasis>. Note that 1kB is equal to 1024
1331 bytes, 1MB is equal to 1024kB, and so on.
1334 <example id="zend.file.transfer.validators.size.example">
1335 <title>Using the Size Validator</title>
1337 <programlisting language="php"><![CDATA[
1338 $upload = new Zend_File_Transfer();
1340 // Limit the size of a file to 40000 bytes
1341 $upload->addValidator('Size', false, 40000);
1343 // Limit the size a given file to maximum 4MB and mimimum 10kB
1344 // Also returns the plain number in case of an error
1345 // instead of a user-friendly number
1346 $upload->addValidator('Size',
1348 array('min' => '10kB',
1350 'bytestring' => false));
1351 ]]></programlisting>
1355 <sect2 id="zend.file.transfer.validators.wordcount">
1356 <title>WordCount Validator</title>
1359 The <classname>WordCount</classname> validator checks for the number of words within
1360 provided files. It supports the following option keys:
1366 <property>min</property>: Sets the minimum number of words to be found.
1372 <property>max</property>: Sets the maximum number of words to be found.
1378 If you initiate this validator with a string or integer, the value will be used as
1379 <property>max</property>. Or you can also use the methods
1380 <methodname>setMin()</methodname> and <methodname>setMax()</methodname> to set both
1381 options afterwards and <methodname>getMin()</methodname> and
1382 <methodname>getMax()</methodname> to retrieve the actual set values.
1385 <example id="zend.file.transfer.validators.wordcount.example">
1386 <title>Using the WordCount Validator</title>
1388 <programlisting language="php"><![CDATA[
1389 $upload = new Zend_File_Transfer();
1391 // Limit the amount of words within files to maximum 2000
1392 $upload->addValidator('WordCount', false, 2000);
1394 // Limit the amount of words within files to maximum 5000 and minimum 1000 words
1395 $upload->addValidator('WordCount', false, array('min' => 1000, 'max' => 5000));
1396 ]]></programlisting>
1401 vim:se ts=4 sw=4 tw=80 et: