PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Hash Validator

The Hash validator checks the content of a transferred file by hashing it. This validator uses the hash extension from PHP. It supports the following options:

  • *: Takes any key or use a numeric array. Sets the hash to validate against.

    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.

  • algorithm: Sets the algorithm to use for hashing the content.

    You can set multiple algorithm by calling the addHash() method multiple times.

Example 421. Using the Hash Validator

<?php
$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'));

Note

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 hash_algos method on the php.net site.

Zend Framework