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

Extension Validator

The Extension validator checks the file extension of the specified files. It supports the following options:

  • *: Sets any key or use a numeric array to check whether the specified file has this file extension.

  • case: 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.

This validator accepts multiple extensions, either as a comma-delimited string, or as an array. You may also use the methods setExtension(), addExtension(), and getExtension() to set and retrieve extension values.

In some cases it is useful to test in a case-sensitive fashion. Therefore the constructor takes a second parameter $case, which, if set to TRUE, will validate the extension in a case-sensitive fashion.

Example 416. Using the Extension Validator

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

Note

Note that this validator only checks the file extension. It does not check the file's MIME type.

Zend Framework