| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!-- Reviewed: no -->
- <sect1 id="zend.file.transfer.validators">
- <title>Validators for Zend_File_Transfer</title>
- <para>
- <classname>Zend_File_Transfer</classname> is delivered with several file-related validators
- which can be used to increase security and prevent possible attacks. Note that these
- validators are only as effective as how effectively you apply them. All validators provided
- with <classname>Zend_File_Transfer</classname> can be found in the
- <classname>Zend_Validator</classname> component and are named
- <classname>Zend_Validate_File_*</classname>. The following validators are available:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <classname>Count</classname>: This validator checks for the number of files. A
- minimum and maximum range can be specified. An error will be thrown if either limit
- is crossed.
- </para>
- </listitem>
- <listitem>
- <para>
- <classname>Crc32</classname>: This validator checks for the crc32 hash value of the
- content from a file. It is based on the <classname>Hash</classname> validator and
- provides a convenient and simple validator that only supports Crc32.
- </para>
- </listitem>
- <listitem>
- <para>
- <classname>ExcludeExtension</classname>: This validator checks the extension of
- files. It will throw an error when an given file has a defined extension. With this
- validator, you can exclude defined extensions from being validated.
- </para>
- </listitem>
- <listitem>
- <para>
- <classname>ExcludeMimeType</classname>: This validator validates the
- <acronym>MIME</acronym> type of files. It can also validate <acronym>MIME</acronym>
- types and will throw an error if the <acronym>MIME</acronym> type of specified file
- matches.
- </para>
- </listitem>
- <listitem>
- <para>
- <classname>Exists</classname>: This validator checks for the existence of files. It
- will throw an error when a specified file does not exist.
- </para>
- </listitem>
- <listitem>
- <para>
- <classname>Extension</classname>: This validator checks the extension of files. It
- will throw an error when a specified file has an undefined extension.
- </para>
- </listitem>
- <listitem>
- <para>
- <classname>FilesSize</classname>: This validator checks the size of validated files.
- It remembers internally the size of all checked files and throws an error when the
- sum of all specified files exceed the defined size. It also provides minimum and
- maximum values.
- </para>
- </listitem>
- <listitem>
- <para>
- <classname>ImageSize</classname>: This validator checks the size of image. It
- validates the width and height and enforces minimum and maximum dimensions.
- </para>
- </listitem>
- <listitem>
- <para>
- <classname>IsCompressed</classname>: This validator checks whether the file is
- compressed. It is based on the <classname>MimeType</classname> validator and
- validates for compression archives like zip or arc. You can also limit it to other
- archives.
- </para>
- </listitem>
- <listitem>
- <para>
- <classname>IsImage</classname>: This validator checks whether the file is an image.
- It is based on the <classname>MimeType</classname> validator and validates for image
- files like jpg or gif. You can also limit it to other image types.
- </para>
- </listitem>
- <listitem>
- <para>
- <classname>Hash</classname>: This validator checks the hash value of the content
- from a file. It supports multiple algorithms.
- </para>
- </listitem>
- <listitem>
- <para>
- <classname>Md5</classname>: This validator checks for the md5 hash value of the
- content from a file. It is based on the <classname>Hash</classname> validator and
- provides a convenient and simple validator that only supports Md5.
- </para>
- </listitem>
- <listitem>
- <para>
- <classname>MimeType</classname>: This validator validates the
- <acronym>MIME</acronym> type of files. It can also validate <acronym>MIME</acronym>
- types and will throw an error if the <acronym>MIME</acronym> type of a specified
- file does not match.
- </para>
- </listitem>
- <listitem>
- <para>
- <classname>NotExists</classname>: This validator checks for the existence of files.
- It will throw an error when an given file does exist.
- </para>
- </listitem>
- <listitem>
- <para>
- <classname>Sha1</classname>: This validator checks for the sha1 hash value of the
- content from a file. It is based on the <classname>Hash</classname> validator and
- provides a convenient and simple validator that only supports sha1.
- </para>
- </listitem>
- <listitem>
- <para>
- <classname>Size</classname>: This validator is able to check files for its file
- size. It provides a minimum and maximum size range and will throw an error when
- either of these thesholds are crossed.
- </para>
- </listitem>
- <listitem>
- <para>
- <classname>Upload</classname>: This validator is internal. It checks if an upload
- has resulted in an error. You must not set it, as it's automatically set by
- <classname>Zend_File_Transfer</classname> itself. So you do not use this validator
- directly. You should only know that it exists.
- </para>
- </listitem>
- <listitem>
- <para>
- <classname>WordCount</classname>: This validator is able to check the number of
- words within files. It provides a minimum and maximum count and will throw an error
- when either of these thresholds are crossed.
- </para>
- </listitem>
- </itemizedlist>
- <sect2 id="zend.file.transfer.validators.usage">
- <title>Using Validators with Zend_File_Transfer</title>
- <para>
- Putting validators to work is quite simple. There are several methods for adding and
- manipulating validators:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <methodname>isValid($files = null)</methodname>: Checks the specified files
- using all validators. <varname>$files</varname> may be either a real filename,
- the element's name or the name of the temporary file.
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>addValidator($validator, $breakChainOnFailure, $options = null,
- $files = null)</methodname>: Adds the specified validator to the validator
- stack (optionally only to the file(s) specified).
- <varname>$validator</varname> may be either an actual validator
- instance or a short name specifying the validator type (e.g., 'Count').
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>addValidators(array $validators, $files =
- null)</methodname>: Adds the specified validators to the stack of
- validators. Each entry may be either a validator
- type/options pair or an array with the key 'validator'
- specifying the validator. All other options will be
- considered validator options for instantiation.
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>setValidators(array $validators, $files =
- null)</methodname>: Overwrites any existing validators with
- the validators specified. The validators should follow the
- syntax for <methodname>addValidators()</methodname>.
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>hasValidator($name)</methodname>: Indicates whether a
- validator has been registered.
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>getValidator($name)</methodname>: Returns a previously
- registered validator.
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>getValidators($files = null)</methodname>: Returns
- registered validators. If <varname>$files</varname> is specified,
- returns validators for that particular file or set of
- files.
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>removeValidator($name)</methodname>: Removes a previously
- registered validator.
- </para>
- </listitem>
- <listitem>
- <para>
- <methodname>clearValidators()</methodname>: Clears all
- registered validators.
- </para>
- </listitem>
- </itemizedlist>
- <example id="zend.file.transfer.validators.usage.example">
- <title>Add Validators to a File Transfer Object</title>
- <programlisting language="php"><![CDATA[
- $upload = new Zend_File_Transfer();
- // Set a file size with 20000 bytes
- $upload->addValidator('Size', false, 20000);
- // Set a file size with 20 bytes minimum and 20000 bytes maximum
- $upload->addValidator('Size', false, array('min' => 20, 'max' => 20000));
- // Set a file size with 20 bytes minimum and 20000 bytes maximum and
- // a file count in one step
- $upload->setValidators(array(
- 'Size' => array('min' => 20, 'max' => 20000),
- 'Count' => array('min' => 1, 'max' => 3),
- ));
- ]]></programlisting>
- </example>
- <example id="zend.file.transfer.validators.usage.exampletwo">
- <title>Limit Validators to Single Files</title>
- <para>
- <methodname>addValidator()</methodname>, <methodname>addValidators()</methodname>,
- and <methodname>setValidators()</methodname> each accept a final
- <varname>$files</varname> argument. This argument can be used to
- specify a particular file or array of files on which to set the
- given validator.
- </para>
- <programlisting language="php"><![CDATA[
- $upload = new Zend_File_Transfer();
- // Set a file size with 20000 bytes and limits it only to 'file2'
- $upload->addValidator('Size', false, 20000, 'file2');
- ]]></programlisting>
- </example>
- <para>
- Normally, you should use the <methodname>addValidators()</methodname> method, which
- can be called multiple times.
- </para>
- <example id="zend.file.transfer.validators.usage.examplemultiple">
- <title>Add Multiple Validators</title>
- <para>
- Often it's simpler just to call <methodname>addValidator()</methodname> multiple
- times with one call for each validator. This also increases readability and makes
- your code more maintainable. All methods provide a fluent interface, so you can
- couple the calls as shown below:
- </para>
- <programlisting language="php"><![CDATA[
- $upload = new Zend_File_Transfer();
- // Set a file size with 20000 bytes
- $upload->addValidator('Size', false, 20000)
- ->addValidator('Count', false, 2)
- ->addValidator('Filessize', false, 25000);
- ]]></programlisting>
- </example>
- <note>
- <para>
- Note that setting the same validator
- multiple times is allowed, but doing so can lead to issues when using
- different options for the same validator.
- </para>
- </note>
- <para>
- Last but not least, you can simply check the files using
- <methodname>isValid()</methodname>.
- </para>
- <example id="zend.file.transfer.validators.usage.exampleisvalid">
- <title>Validate the Files</title>
- <para>
- <methodname>isValid()</methodname> accepts the file name of the uploaded or
- downloaded file, the temporary file name and or the name of the form element. If
- no parameter or null is given all files will be validated
- </para>
- <programlisting language="php"><![CDATA[
- $upload = new Zend_File_Transfer();
- // Set a file size with 20000 bytes
- $upload->addValidator('Size', false, 20000)
- ->addValidator('Count', false, 2)
- ->addValidator('Filessize', false, 25000);
- if (!$upload->isValid()) {
- print "Validation failure";
- }
- ]]></programlisting>
- </example>
- <note>
- <para>
- Note that <methodname>isValid()</methodname> will be called automatically when you
- receive the files and have not called it previously.
- </para>
- </note>
- <para>
- When validation has failed it is a good idea to get information about the
- problems found. To get this information, you can use the methods
- <methodname>getMessages()</methodname> which returns all validation messages as array,
- <methodname>getErrors()</methodname> which returns all error codes, and
- <methodname>hasErrors()</methodname> which returns <constant>TRUE</constant> as soon as
- a validation error has been found.
- </para>
- </sect2>
- <sect2 id="zend.file.transfer.validators.count">
- <title>Count Validator</title>
- <para>
- The <classname>Count</classname> validator checks for the number of files which are
- provided. It supports the following option keys:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <property>min</property>: Sets the minimum number of files to transfer.
- </para>
- <note>
- <para>
- When using this option you must give the minimum number of files when
- calling this validator the first time; otherwise you will get an error in
- return.
- </para>
- </note>
- <para>
- With this option you can define the minimum number of files you expect to
- receive.
- </para>
- </listitem>
- <listitem>
- <para>
- <property>max</property>: Sets the maximum number of files to transfer.
- </para>
- <para>
- With this option you can limit the number of files which are accepted but also
- detect a possible attack when more files are given than defined in your form.
- </para>
- </listitem>
- </itemizedlist>
- <para>
- If you initiate this validator with a string or integer, the value will be used as
- <property>max</property>. Or you can also use the methods
- <methodname>setMin()</methodname> and <methodname>setMax()</methodname> to set both
- options afterwards and <methodname>getMin()</methodname> and
- <methodname>getMax()</methodname> to retrieve the actual set values.
- </para>
- <example id="zend.file.transfer.validators.count.example">
- <title>Using the Count Validator</title>
- <programlisting language="php"><![CDATA[
- $upload = new Zend_File_Transfer();
- // Limit the amount of files to maximum 2
- $upload->addValidator('Count', false, 2);
- // Limit the amount of files to maximum 5 and minimum 1 file
- $upload->addValidator('Count', false, array('min' =>1, 'max' => 5));
- ]]></programlisting>
- </example>
- <note>
- <para>
- Note that this validator stores the number of checked files internally. The file
- which exceeds the maximum will be returned as error.
- </para>
- </note>
- </sect2>
- <sect2 id="zend.file.transfer.validators.crc32">
- <title>Crc32 Validator</title>
- <para>
- The <classname>Crc32</classname> validator checks the content of a transferred file by
- hashing it. This validator uses the hash extension from <acronym>PHP</acronym> with the
- crc32 algorithm. It supports the following options:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <property>*</property>: Sets any key or use a numeric array. The values will be
- used as hash to validate against.
- </para>
- <para>
- You can set multiple hashes by using different keys. Each will be checked and
- the validation will fail only if all values fail.
- </para>
- </listitem>
- </itemizedlist>
- <example id="zend.file.transfer.validators.crc32.example">
- <title>Using the Crc32 Validator</title>
- <programlisting language="php"><![CDATA[
- $upload = new Zend_File_Transfer();
- // Checks whether the content of the uploaded file has the given hash
- $upload->addValidator('Crc32', false, '3b3652f');
- // Limits this validator to two different hashes
- $upload->addValidator('Crc32', false, array('3b3652f', 'e612b69'));
- ]]></programlisting>
- </example>
- </sect2>
- <sect2 id="zend.file.transfer.validators.excludeextension">
- <title>ExcludeExtension Validator</title>
- <para>
- The <classname>ExcludeExtension</classname> validator checks the file extension of the
- specified files. It supports the following options:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <property>*</property>: Sets any key or use a numeric array. The values will be
- used to check whether the given file does not use this file extension.
- </para>
- </listitem>
- <listitem>
- <para>
- <property>case</property>: Sets a boolean indicating whether validation should
- be case-sensitive. The default is not case sensitive. Note that this key can be
- applied to for all available extensions.
- </para>
- </listitem>
- </itemizedlist>
- <para>
- This validator accepts multiple extensions, either as a comma-delimited string, or as
- an array. You may also use the methods <methodname>setExtension()</methodname>,
- <methodname>addExtension()</methodname>, and <methodname>getExtension()</methodname>
- to set and retrieve extensions.
- </para>
- <para>
- In some cases it is useful to match in a case-sensitive fashion. So the constructor
- allows a second parameter called <varname>$case</varname> which, if set to
- <constant>TRUE</constant>, validates the extension by comparing it with the specified
- values in a case-sensitive fashion.
- </para>
- <example id="zend.file.transfer.validators.excludeextension.example">
- <title>Using the ExcludeExtension Validator</title>
- <programlisting language="php"><![CDATA[
- $upload = new Zend_File_Transfer();
- // Do not allow files with extension php or exe
- $upload->addValidator('ExcludeExtension', false, 'php,exe');
- // Do not allow files with extension php or exe, but use array notation
- $upload->addValidator('ExcludeExtension', false, array('php', 'exe'));
- // Check in a case-sensitive fashion
- $upload->addValidator('ExcludeExtension',
- false,
- array('php', 'exe', 'case' => true));
- $upload->addValidator('ExcludeExtension',
- false,
- array('php', 'exe', 'case' => true));
- ]]></programlisting>
- </example>
- <note>
- <para>
- Note that this validator only checks the file extension. It does not check the
- file's <acronym>MIME</acronym> type.
- </para>
- </note>
- </sect2>
- <sect2 id="zend.file.transfer.validators.excludemimetype">
- <title>ExcludeMimeType Validator</title>
- <para>
- The <classname>ExcludeMimeType</classname> validator checks the <acronym>MIME</acronym>
- type of transferred files. It supports the following options:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <property>*</property>: Sets any key individually or use a numeric array. Sets
- the <acronym>MIME</acronym> type to validate against.
- </para>
- <para>
- With this option you can define the <acronym>MIME</acronym> type of files that
- are not to be accepted.
- </para>
- </listitem>
- <listitem>
- <para>
- <property>headerCheck</property>: If set to <constant>TRUE</constant> this
- option will check the <acronym>HTTP</acronym> Information for the file type when
- the <emphasis>fileInfo</emphasis> or <emphasis>mimeMagic</emphasis> extensions
- can not be found. The default value for this option is
- <constant>FALSE</constant>.
- </para>
- </listitem>
- </itemizedlist>
- <para>
- This validator accepts multiple <acronym>MIME</acronym> types, either as a
- comma-delimited string, or as an array. You may also use the methods
- <methodname>setMimeType()</methodname>, <methodname>addMimeType()</methodname>, and
- <methodname>getMimeType()</methodname> to set and retrieve the <acronym>MIME</acronym>
- types.
- </para>
- <example id="zend.file.transfer.validators.excludemimetype.example">
- <title>Using the ExcludeMimeType Validator</title>
- <programlisting language="php"><![CDATA[
- $upload = new Zend_File_Transfer();
- // Does not allow MIME type of gif images for all files
- $upload->addValidator('ExcludeMimeType', false, 'image/gif');
- // Does not allow MIME type of gif and jpg images for all given files
- $upload->addValidator('ExcludeMimeType', false, array('image/gif',
- 'image/jpeg');
- // Does not allow MIME type of the group images for all given files
- $upload->addValidator('ExcludeMimeType', false, 'image');
- ]]></programlisting>
- </example>
- <para>
- The above example shows that it is also possible to disallow groups of
- <acronym>MIME</acronym> types. For example, to disallow all images, just use 'image' as
- the <acronym>MIME</acronym> type. This can be used for all groups of
- <acronym>MIME</acronym> types like 'image', 'audio', 'video', 'text', etc.
- </para>
- <note>
- <para>
- Note that disallowing groups of <acronym>MIME</acronym> types will disallow all
- members of this group even if this is not intentional. When you disallow 'image' you
- will disallow all types of images like 'image/jpeg' or 'image/vasa'. When you are
- not sure if you want to disallow all types, you should disallow only specific
- <acronym>MIME</acronym> types instead of complete groups.
- </para>
- </note>
- </sect2>
- <sect2 id="zend.file.transfer.validators.exists">
- <title>Exists Validator</title>
- <para>
- The <classname>Exists</classname> validator checks for the existence of specified
- files. It supports the following options:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <property>*</property>: Sets any key or use a numeric array to check if the
- specific file exists in the given directory.
- </para>
- </listitem>
- </itemizedlist>
- <para>
- This validator accepts multiple directories, either as a comma-delimited string, or as
- an array. You may also use the methods <methodname>setDirectory()</methodname>,
- <methodname>addDirectory()</methodname>, and <methodname>getDirectory()</methodname>
- to set and retrieve directories.
- </para>
- <example id="zend.file.transfer.validators.exists.example">
- <title>Using the Exists Validator</title>
- <programlisting language="php"><![CDATA[
- $upload = new Zend_File_Transfer();
- // Add the temp directory to check for
- $upload->addValidator('Exists', false, '\temp');
- // Add two directories using the array notation
- $upload->addValidator('Exists',
- false,
- array('\home\images', '\home\uploads'));
- ]]></programlisting>
- </example>
- <note>
- <para>
- Note that this validator checks whether the specified file exists in all of the
- given directories. The validation will fail if the file does not exist in any of the
- given directories.
- </para>
- </note>
- </sect2>
- <sect2 id="zend.file.transfer.validators.extension">
- <title>Extension Validator</title>
- <para>
- The <classname>Extension</classname> validator checks the file extension of the
- specified files. It supports the following options:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <property>*</property>: Sets any key or use a numeric array to check whether the
- specified file has this file extension.
- </para>
- </listitem>
- <listitem>
- <para>
- <property>case</property>: Sets whether validation should be done in a
- case-sensitive fashion. The default is no case sensitivity. Note the this key is
- used for all given extensions.
- </para>
- </listitem>
- </itemizedlist>
- <para>
- This validator accepts multiple extensions, either as a comma-delimited string, or as an
- array. You may also use the methods <methodname>setExtension()</methodname>,
- <methodname>addExtension()</methodname>, and <methodname>getExtension()</methodname> to
- set and retrieve extension values.
- </para>
- <para>
- In some cases it is useful to test in a case-sensitive fashion. Therefore the
- constructor takes a second parameter <varname>$case</varname>, which, if set to
- <constant>TRUE</constant>, will validate the extension in a case-sensitive fashion.
- </para>
- <example id="zend.file.transfer.validators.extension.example">
- <title>Using the Extension Validator</title>
- <programlisting language="php"><![CDATA[
- $upload = new Zend_File_Transfer();
- // Limit the extensions to jpg and png files
- $upload->addValidator('Extension', false, 'jpg,png');
- // Limit the extensions to jpg and png files but use array notation
- $upload->addValidator('Extension', false, array('jpg', 'png'));
- // Check case sensitive
- $upload->addValidator('Extension', false, array('mo', 'png', 'case' => true));
- if (!$upload->isValid('C:\temp\myfile.MO')) {
- print 'Not valid because MO and mo do not match with case sensitivity;
- }
- ]]></programlisting>
- </example>
- <note>
- <para>
- Note that this validator only checks the file extension. It does not check the
- file's <acronym>MIME</acronym> type.
- </para>
- </note>
- </sect2>
- <sect2 id="zend.file.transfer.validators.filessize">
- <title>FilesSize Validator</title>
- <para>
- The <classname>FilesSize</classname> validator checks for the aggregate size of all
- transferred files. It supports the following options:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <property>min</property>: Sets the minimum aggregate file size.
- This option defines the minimum aggregate file size to be transferred.
- </para>
- </listitem>
- <listitem>
- <para>
- <property>max</property>: Sets the maximum aggregate file size.
- </para>
- <para>
- This option limits the aggregate file size of all transferred files, but not the
- file size of individual files.
- </para>
- </listitem>
- <listitem>
- <para>
- <property>bytestring</property>: Defines whether a failure is to return a
- user-friendly number or the plain file size.
- </para>
- <para>
- This option defines whether the user sees '10864' or '10MB'. The default value
- is <constant>TRUE</constant>, so '10MB' is returned if you did not specify
- otherwise.
- </para>
- </listitem>
- </itemizedlist>
- <para>
- You can initialize this validator with a string, which will then be used to set the
- <property>max</property> option. You can also use the methods
- <methodname>setMin()</methodname> and <methodname>setMax()</methodname> to set both
- options after construction, along with <methodname>getMin()</methodname> and
- <methodname>getMax()</methodname> to retrieve the values that have been set previously.
- </para>
- <para>
- The size itself is also accepted in SI notation as handled by most operating systems.
- That is, instead of specifying <emphasis>20000 bytes</emphasis>,
- <emphasis>20kB</emphasis> may be given. All file sizes are converted using 1024 as the
- base value. The following Units are accepted: <emphasis>kB</emphasis>,
- <emphasis>MB</emphasis>, <emphasis>GB</emphasis>, <emphasis>TB</emphasis>,
- <emphasis>PB</emphasis> and <emphasis>EB</emphasis>. Note that 1kB is equal to 1024
- bytes, 1MB is equal to 1024kB, and so on.
- </para>
- <example id="zend.file.transfer.validators.filessize.example">
- <title>Using the FilesSize Validator</title>
- <programlisting language="php"><![CDATA[
- $upload = new Zend_File_Transfer();
- // Limit the size of all files to be uploaded to 40000 bytes
- $upload->addValidator('FilesSize', false, 40000);
- // Limit the size of all files to be uploaded to maximum 4MB and mimimum 10kB
- $upload->addValidator('FilesSize',
- false,
- array('min' => '10kB', 'max' => '4MB'));
- // As before, but returns the plain file size instead of a user-friendly string
- $upload->addValidator('FilesSize',
- false,
- array('min' => '10kB',
- 'max' => '4MB',
- 'bytestring' => false));
- ]]></programlisting>
- </example>
- <note>
- <para>
- Note that this validator internally stores the file size of checked files. The file
- which exceeds the size will be returned as an error.
- </para>
- </note>
- </sect2>
- <sect2 id="zend.file.transfer.validators.imagesize">
- <title>ImageSize Validator</title>
- <para>
- The <classname>ImageSize</classname> validator checks the size of image files.
- It supports the following options:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <property>minheight</property>: Sets the minimum image height.
- </para>
- </listitem>
- <listitem>
- <para>
- <property>maxheight</property>: Sets the maximum image height.
- </para>
- </listitem>
- <listitem>
- <para>
- <property>minwidth</property>: Sets the minimum image width.
- </para>
- </listitem>
- <listitem>
- <para>
- <property>maxwidth</property>: Sets the maximum image width.
- </para>
- </listitem>
- </itemizedlist>
- <para>
- The methods <methodname>setImageMin()</methodname> and
- <methodname>setImageMax()</methodname> also set both minimum and maximum values, while
- the methods <methodname>getMin()</methodname> and <methodname>getMax()</methodname>
- return the currently set values.
- </para>
- <para>
- For your convenience there are also the <methodname>setImageWidth()</methodname> and
- <methodname>setImageHeight()</methodname> methods, which set the minimum and maximum
- height and width of the image file. They, too, have corresponding
- <methodname>getImageWidth()</methodname> and <methodname>getImageHeight()</methodname>
- methods to retrieve the currently set values.
- </para>
- <para>
- To bypass validation of a particular dimension, the relevent option simply should not be
- set.
- </para>
- <example id="zend.file.transfer.validators.imagesize.example">
- <title>Using the ImageSize Validator</title>
- <programlisting language="php"><![CDATA[
- $upload = new Zend_File_Transfer();
- // Limit the size of a image to a height of 100-200 and a width of
- // 40-80 pixel
- $upload->addValidator('ImageSize', false,
- array('minwidth' => 40,
- 'maxwidth' => 80,
- 'minheight' => 100,
- 'maxheight' => 200)
- );
- // Reset the width for validation
- $upload->setImageWidth(array('minwidth' => 20, 'maxwidth' => 200));
- ]]></programlisting>
- </example>
- </sect2>
- <sect2 id="zend.file.transfer.validators.iscompressed">
- <title>IsCompressed Validator</title>
- <para>
- The <classname>IsCompressed</classname> validator checks if a transferred file is a
- compressed archive, such as zip or arc. This validator is based on the
- <classname>MimeType</classname> validator and supports the same methods and options.
- You may also limit this validator to particular compression types with the methods
- described there.
- </para>
- <example id="zend.file.transfer.validators.iscompressed.example">
- <title>Using the IsCompressed Validator</title>
- <programlisting language="php"><![CDATA[
- $upload = new Zend_File_Transfer();
- // Checks is the uploaded file is a compressed archive
- $upload->addValidator('IsCompressed', false);
- // Limits this validator to zip files only
- $upload->addValidator('IsCompressed', false, array('application/zip'));
- // Limits this validator to zip files only using simpler notation
- $upload->addValidator('IsCompressed', false, 'zip');
- ]]></programlisting>
- </example>
- <note>
- <para>
- Note that there is no check if you set a <acronym>MIME</acronym> type that is not a
- archive. For example, it would be possible to define gif files to be accepted by
- this validator. Using the 'MimeType' validator for files which are not archived will
- result in more readable code.
- </para>
- </note>
- </sect2>
- <sect2 id="zend.file.transfer.validators.isimage">
- <title>IsImage Validator</title>
- <para>
- The <classname>IsImage</classname> validator checks if a transferred file is a image
- file, such as gif or jpeg. This validator is based on the
- <classname>MimeType</classname> validator and supports the same methods and options.
- You can limit this validator to particular image types with the methods described there.
- </para>
- <example id="zend.file.transfer.validators.isimage.example">
- <title>Using the IsImage Validator</title>
- <programlisting language="php"><![CDATA[
- $upload = new Zend_File_Transfer();
- // Checks whether the uploaded file is a image file
- $upload->addValidator('IsImage', false);
- // Limits this validator to gif files only
- $upload->addValidator('IsImage', false, array('application/gif'));
- // Limits this validator to jpeg files only using a simpler notation
- $upload->addValidator('IsImage', false, 'jpeg');
- ]]></programlisting>
- </example>
- <note>
- <para>
- Note that there is no check if you set a <acronym>MIME</acronym> type that is not an
- image. For example, it would be possible to define zip files to be accepted by this
- validator. Using the 'MimeType' validator for files which are not images will result
- in more readable code.
- </para>
- </note>
- </sect2>
- <sect2 id="zend.file.transfer.validators.hash">
- <title>Hash Validator</title>
- <para>
- The <classname>Hash</classname> validator checks the content of a transferred file by
- hashing it. This validator uses the hash extension from <acronym>PHP</acronym>. It
- supports the following options:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <property>*</property>: Takes any key or use a numeric array. Sets the hash to
- validate against.
- </para>
- <para>
- 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.
- </para>
- </listitem>
- <listitem>
- <para>
- <property>algorithm</property>: Sets the algorithm to use for hashing the
- content.
- </para>
- <para>
- You can set multiple algorithm by calling the <methodname>addHash()</methodname>
- method multiple times.
- </para>
- </listitem>
- </itemizedlist>
- <example id="zend.file.transfer.validators.hash.example">
- <title>Using the Hash Validator</title>
- <programlisting language="php"><![CDATA[
- $upload = new Zend_File_Transfer();
- // Checks if the content of the uploaded file contains the given hash
- $upload->addValidator('Hash', false, '3b3652f');
- // Limits this validator to two different hashes
- $upload->addValidator('Hash', false, array('3b3652f', 'e612b69'));
- // Sets a different algorithm to check against
- $upload->addValidator('Hash',
- false,
- array('315b3cd8273d44912a7',
- 'algorithm' => 'md5'));
- ]]></programlisting>
- </example>
- <note>
- <para>
- This validator supports about 34 different hash algorithms. The most common include
- 'crc32', 'md5' and 'sha1'. A comprehesive list of supports hash algorithms can be
- found at the <ulink url="http://php.net/hash_algos">hash_algos method</ulink> on the
- <ulink url="http://php.net">php.net site</ulink>.
- </para>
- </note>
- </sect2>
- <sect2 id="zend.file.transfer.validators.md5">
- <title>Md5 Validator</title>
- <para>
- The <classname>Md5</classname> validator checks the content of a transferred file by
- hashing it. This validator uses the hash extension for <acronym>PHP</acronym> with the
- md5 algorithm. It supports the following options:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <property>*</property>: Takes any key or use a numeric array.
- </para>
- <para>
- 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.
- </para>
- </listitem>
- </itemizedlist>
- <example id="zend.file.transfer.validators.md5.example">
- <title>Using the Md5 Validator</title>
- <programlisting language="php"><![CDATA[
- $upload = new Zend_File_Transfer();
- // Checks if the content of the uploaded file has the given hash
- $upload->addValidator('Md5', false, '3b3652f336522365223');
- // Limits this validator to two different hashes
- $upload->addValidator('Md5',
- false,
- array('3b3652f336522365223',
- 'eb3365f3365ddc65365'));
- ]]></programlisting>
- </example>
- </sect2>
- <sect2 id="zend.file.transfer.validators.mimetype">
- <title>MimeType Validator</title>
- <para>
- The <classname>MimeType</classname> validator checks the <acronym>MIME</acronym> type of
- transferred files. It supports the following options:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <property>*</property>: Sets any key or use a numeric array. Sets the
- <acronym>MIME</acronym> type to validate against.
- </para>
- <para>
- Defines the <acronym>MIME</acronym> type of files to be accepted.
- </para>
- </listitem>
- <listitem>
- <para>
- <property>headerCheck</property>: If set to <constant>TRUE</constant> this
- option will check the <acronym>HTTP</acronym> Information for the file type when
- the <emphasis>fileInfo</emphasis> or <emphasis>mimeMagic</emphasis> extensions
- can not be found. The default value for this option is
- <constant>FALSE</constant>.
- </para>
- </listitem>
- <listitem>
- <para>
- <property>magicfile</property>: The magicfile to be used.
- </para>
- <para>
- With this option you can define which magicfile to use. When it's not set or
- empty, the MAGIC constant will be used instead. This option is available since
- Zend Framework 1.7.1.
- </para>
- </listitem>
- </itemizedlist>
- <para>
- This validator accepts multiple <acronym>MIME</acronym> type, either as a
- comma-delimited string, or as an array. You may also use the methods
- <methodname>setMimeType()</methodname>, <methodname>addMimeType()</methodname>, and
- <methodname>getMimeType()</methodname> to set and retrieve <acronym>MIME</acronym>
- type.
- </para>
- <para>
- You can also set the magicfile which shall be used by fileinfo with the 'magicfile'
- option. Additionally there are convenient <methodname>setMagicFile()</methodname> and
- <methodname>getMagicFile()</methodname> methods which allow later setting and retrieving
- of the magicfile parameter. This methods are available since Zend Framework 1.7.1.
- </para>
- <example id="zend.file.transfer.validators.mimetype.example">
- <title>Using the MimeType Validator</title>
- <programlisting language="php"><![CDATA[
- $upload = new Zend_File_Transfer();
- // Limit the MIME type of all given files to gif images
- $upload->addValidator('MimeType', false, 'image/gif');
- // Limit the MIME type of all given files to gif and jpeg images
- $upload->addValidator('MimeType', false, array('image/gif', 'image/jpeg');
- // Limit the MIME type of all given files to the group images
- $upload->addValidator('MimeType', false, 'image');
- // Use a different magicfile
- $upload->addValidator('MimeType',
- false,
- array('image',
- 'magicfile' => '/path/to/magicfile.mgx'));
- ]]></programlisting>
- </example>
- <para>
- The above example shows that it is also possible to limit the accepted
- <acronym>MIME</acronym> type to a group of <acronym>MIME</acronym> types. To allow all
- images just use 'image' as <acronym>MIME</acronym> type. This can be used for all groups
- of <acronym>MIME</acronym> types like 'image', 'audio', 'video', 'text, and so on.
- </para>
- <note>
- <para>
- Note that allowing groups of <acronym>MIME</acronym> types will accept all members
- of this group even if your application does not support them. When you allow 'image'
- you will also get 'image/xpixmap' or 'image/vasa' which could be problematic. When
- you are not sure if your application supports all types you should better allow only
- defined <acronym>MIME</acronym> types instead of the complete group.
- </para>
- </note>
- <note>
- <para>
- This component will use the <classname>FileInfo</classname> extension if it is
- available. If it's not, it will degrade to the
- <methodname>mime_content_type()</methodname> function. And if the function call
- fails it will use the <acronym>MIME</acronym> type which is given by
- <acronym>HTTP</acronym>.
- </para>
- <para>
- You should be aware of possible security problems when you have whether
- <classname>FileInfo</classname> nor <methodname>mime_content_type()</methodname>
- available. The <acronym>MIME</acronym> type given by <acronym>HTTP</acronym> is not
- secure and can be easily manipulated.
- </para>
- </note>
- </sect2>
- <sect2 id="zend.file.transfer.validators.notexists">
- <title>NotExists Validator</title>
- <para>
- The <classname>NotExists</classname> validator checks for the existence of the provided
- files. It supports the following options:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <property>*</property>: Set any key or use a numeric array. Checks whether the
- file exists in the given directory.
- </para>
- </listitem>
- </itemizedlist>
- <para>
- This validator accepts multiple directories either as a comma-delimited string, or as an
- array. You may also use the methods <methodname>setDirectory()</methodname>,
- <methodname>addDirectory()</methodname>, and <methodname>getDirectory()</methodname> to
- set and retrieve directories.
- </para>
- <example id="zend.file.transfer.validators.notexists.example">
- <title>Using the NotExists Validator</title>
- <programlisting language="php"><![CDATA[
- $upload = new Zend_File_Transfer();
- // Add the temp directory to check
- $upload->addValidator('NotExists', false, '\temp');
- // Add two directories using the array notation
- $upload->addValidator('NotExists', false,
- array('\home\images',
- '\home\uploads')
- );
- ]]></programlisting>
- </example>
- <note>
- <para>
- Note that this validator checks if the file does not exist in all of the provided
- directories. The validation will fail if the file does exist in any of the given
- directories.
- </para>
- </note>
- </sect2>
- <sect2 id="zend.file.transfer.validators.sha1">
- <title>Sha1 Validator</title>
- <para>
- The <classname>Sha1</classname> validator checks the content of a transferred file by
- hashing it. This validator uses the hash extension for <acronym>PHP</acronym> with the
- sha1 algorithm. It supports the following options:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <property>*</property>: Takes any key or use a numeric array.
- </para>
- <para>
- 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.
- </para>
- </listitem>
- </itemizedlist>
- <example id="zend.file.transfer.validators.sha1.example">
- <title>Using the sha1 Validator</title>
- <programlisting language="php"><![CDATA[
- $upload = new Zend_File_Transfer();
- // Checks if the content of the uploaded file has the given hash
- $upload->addValidator('sha1', false, '3b3652f336522365223');
- // Limits this validator to two different hashes
- $upload->addValidator('Sha1',
- false, array('3b3652f336522365223',
- 'eb3365f3365ddc65365'));
- ]]></programlisting>
- </example>
- </sect2>
- <sect2 id="zend.file.transfer.validators.size">
- <title>Size Validator</title>
- <para>
- The <classname>Size</classname> validator checks for the size of a single file. It
- supports the following options:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <property>min</property>: Sets the minimum file size.
- </para>
- </listitem>
- <listitem>
- <para>
- <property>max</property>: Sets the maximum file size.
- </para>
- </listitem>
- <listitem>
- <para>
- <property>bytestring</property>: Defines whether a failure is returned with a
- user-friendly number, or with the plain file size.
- </para>
- <para>
- With this option you can define if the user gets '10864' or '10MB'. Default
- value is <constant>TRUE</constant> which returns '10MB'.
- </para>
- </listitem>
- </itemizedlist>
- <para>
- You can initialize this validator with a string, which will then be used to set the
- <property>max</property> option. You can also use the methods
- <methodname>setMin()</methodname> and <methodname>setMax()</methodname> to set both
- options after construction, along with <methodname>getMin()</methodname> and
- <methodname>getMax()</methodname> to retrieve the values that have been set previously.
- </para>
- <para>
- The size itself is also accepted in SI notation as handled by most operating systems.
- That is, instead of specifying <emphasis>20000 bytes</emphasis>,
- <emphasis>20kB</emphasis> may be given. All file sizes are converted using 1024 as the
- base value. The following Units are accepted: <emphasis>kB</emphasis>,
- <emphasis>MB</emphasis>, <emphasis>GB</emphasis>, <emphasis>TB</emphasis>,
- <emphasis>PB</emphasis> and <emphasis>EB</emphasis>. Note that 1kB is equal to 1024
- bytes, 1MB is equal to 1024kB, and so on.
- </para>
- <example id="zend.file.transfer.validators.size.example">
- <title>Using the Size Validator</title>
- <programlisting language="php"><![CDATA[
- $upload = new Zend_File_Transfer();
- // Limit the size of a file to 40000 bytes
- $upload->addValidator('Size', false, 40000);
- // Limit the size a given file to maximum 4MB and mimimum 10kB
- // Also returns the plain number in case of an error
- // instead of a user-friendly number
- $upload->addValidator('Size',
- false,
- array('min' => '10kB',
- 'max' => '4MB',
- 'bytestring' => false));
- ]]></programlisting>
- </example>
- </sect2>
- <sect2 id="zend.file.transfer.validators.wordcount">
- <title>WordCount Validator</title>
- <para>
- The <classname>WordCount</classname> validator checks for the number of words within
- provided files. It supports the following option keys:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <property>min</property>: Sets the minimum number of words to be found.
- </para>
- </listitem>
- <listitem>
- <para>
- <property>max</property>: Sets the maximum number of words to be found.
- </para>
- </listitem>
- </itemizedlist>
- <para>
- If you initiate this validator with a string or integer, the value will be used as
- <property>max</property>. Or you can also use the methods
- <methodname>setMin()</methodname> and <methodname>setMax()</methodname> to set both
- options afterwards and <methodname>getMin()</methodname> and
- <methodname>getMax()</methodname> to retrieve the actual set values.
- </para>
- <example id="zend.file.transfer.validators.wordcount.example">
- <title>Using the WordCount Validator</title>
- <programlisting language="php"><![CDATA[
- $upload = new Zend_File_Transfer();
- // Limit the amount of words within files to maximum 2000
- $upload->addValidator('WordCount', false, 2000);
- // Limit the amount of words within files to maximum 5000 and minimum 1000 words
- $upload->addValidator('WordCount', false, array('min' => 1000, 'max' => 5000));
- ]]></programlisting>
- </example>
- </sect2>
- </sect1>
- <!--
- vim:se ts=4 sw=4 tw=80 et:
- -->
|