| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394 |
- <?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>
- <code>Count</code>: 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>
- <code>Crc32</code>: This validator checks for the crc32 hash value of the content
- from a file. It is based on the <code>Hash</code> validator and provides a
- convenient and simple validator that only supports Crc32.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>ExcludeExtension</code>: 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>
- <code>ExcludeMimeType</code>: 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>
- <code>Exists</code>: This validator checks for the existence of files. It will throw
- an error when a specified file does not exist.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>Extension</code>: This validator checks the extension of files. It will throw
- an error when a specified file has an undefined extension.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>FilesSize</code>: 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>
- <code>ImageSize</code>: This validator checks the size of image. It validates the
- width and height and enforces minimum and maximum dimensions.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>IsCompressed</code>: This validator checks whether the file is compressed. It
- is based on the <code>MimeType</code> validator and validates for compression
- archives like zip or arc. You can also limit it to other archives.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>IsImage</code>: This validator checks whether the file is an image. It is
- based on the <code>MimeType</code> validator and validates for image files like jpg
- or gif. You can also limit it to other image types.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>Hash</code>: This validator checks the hash value of the content from a file.
- It supports multiple algorithms.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>Md5</code>: This validator checks for the md5 hash value of the content from a
- file. It is based on the <code>Hash</code> validator and provides a convenient and
- simple validator that only supports Md5.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>MimeType</code>: 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>
- <code>NotExists</code>: This validator checks for the existence of files. It will
- throw an error when an given file does exist.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>Sha1</code>: This validator checks for the sha1 hash value of the content from
- a file. It is based on the <code>Hash</code> validator and provides a convenient and
- simple validator that only supports sha1.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>Size</code>: 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>
- <code>Upload</code>: 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>
- <code>WordCount</code>: 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>
- <code>addValidator($validator, $breakChainOnFailure, $options = null, $files =
- null)</code>: 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>
- <code>addValidators(array $validators, $files =
- null)</code>: 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>
- <code>setValidators(array $validators, $files =
- null)</code>: 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 <code>Count</code> validator checks for the number of files which are provided. It
- supports the following option keys:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <code>min</code>: 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>
- <code>max</code>: 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
- <code>max</code>. 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 <code>Crc32</code> 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>
- <code>*</code>: 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 <code>ExcludeExtension</code> validator checks the file extension of the specified
- files. It supports the following options:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <code>*</code>: 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>
- <code>case</code>: 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 <code>ExcludeMimeType</code> validator checks the <acronym>MIME</acronym> type of
- transferred files. It supports the following options:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <code>*</code>: 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>
- <code>headerCheck</code>: 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 <code>Exists</code> validator checks for the existence of specified files. It
- supports the following options:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <code>*</code>: 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 <code>Extension</code> validator checks the file extension of the specified files.
- It supports the following options:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <code>*</code>: Sets any key or use a numeric array to check whether the
- specified file has this file extension.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>case</code>: 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 <code>FilesSize</code> validator checks for the aggregate size of all transferred
- files. It supports the following options:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <code>min</code>: Sets the minimum aggregate file size.
- This option defines the minimum aggregate file size to be transferred.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>max</code>: 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>
- <code>bytestring</code>: 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
- <code>max</code> 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: <code>kB</code>, <code>MB</code>,
- <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.
- </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 <code>ImageSize</code> validator checks the size of image files.
- It supports the following options:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <code>minheight</code>: Sets the minimum image height.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>maxheight</code>: Sets the maximum image height.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>minwidth</code>: Sets the minimum image width.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>maxwidth</code>: 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 <code>IsCompressed</code> validator checks if a transferred file is a compressed
- archive, such as zip or arc. This validator is based on the <code>MimeType</code>
- 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 <code>IsImage</code> validator checks if a transferred file is a image file, such as
- gif or jpeg. This validator is based on the <code>MimeType</code> 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 <code>Hash</code> 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>
- <code>*</code>: 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>
- <code>algorithm</code>: 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 <code>Md5</code> 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>
- <code>*</code>: 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 <code>MimeType</code> validator checks the <acronym>MIME</acronym> type of transferred files.
- It supports the following options:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <code>*</code>: 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>
- <code>headerCheck</code>: 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>
- <code>magicfile</code>: 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 <code>fileinfo</code> extension if it is available. If
- it's not, it will degrade to the <code>mime_content_type</code> 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
- <code>fileinfo</code> nor <code>mime_content_type</code> 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 <code>NotExists</code> validator checks for the existence of the provided files. It
- supports the following options:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <code>*</code>: 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 <code>Sha1</code> 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>
- <code>*</code>: 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 <code>Size</code> validator checks for the size of a single file. It
- supports the following options:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <code>min</code>: Sets the minimum file size.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>max</code>: Sets the maximum file size.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>bytestring</code>: 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
- <code>max</code> 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: <code>kB</code>, <code>MB</code>,
- <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.
- </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 <code>WordCount</code> validator checks for the number of words within provided
- files. It supports the following option keys:
- </para>
- <itemizedlist>
- <listitem>
- <para>
- <code>min</code>: Sets the minimum number of words to be found.
- </para>
- </listitem>
- <listitem>
- <para>
- <code>max</code>: 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
- <code>max</code>. 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:
- -->
|