FORMfields, the premiere web framework
Login
Support Home | Knowledge Base | Documentation | Forums | Contact Us
Info
FORMfields & FORMgen :: Field Customizations

Limiting the Extensions of the Files Uploaded by the UploadField

(1241 views. Last Updated: 2007-11-03 9:46 AM)

There are two ways to limit the file extensions of the files uploaded with the UploadField:
1. By specifying prohibited extensions
Or,
2. By specifying allowed extensions

1. To specify prohibited extensions:
a. Generate a form with FORMgen that has an UploadField called "upload"
b. To add extensions to the list that is normally prohibited, edit this form and replace the following code:
$ffForm->addField(new UploadField("upload", "Upload", FORM_FIELD_REQUIRED, 2000000));
with:
$ffForm->addField(new UploadField("upload", "Upload", FORM_FIELD_REQUIRED, 2000000));
$ffForm->formFields["upload"]->prohibitedExts = array_merge($ffForm->formFields["upload"]->prohibitedExts, array("txt", "doc"));
This will prohibit the uploading of ".txt" and ".doc" files.

2. To specify allowed extensions:
a. 1st apply a patch to the UploadField to support allowed extensions:
i. Edit FORMfields/src/fields/UploadField.php and add:
/**
 * An array of partial extensions that can be uploaded
 * with this field. If this array is not null, it will override
 * prohibitedExts.
 * @since FORMfields v3.0.20071103
 */
var $allowedExts = null;
after the:
var $prohibitedExts = array("php", "phtml", "asp", "jsp", "py", "cfm");
ii. Edit FORMfields/src/fields/UploadField.php and replace:
function validExtension($ext) 
{
    foreach($this->prohibitedExts as $badExt) {
        if (FfHelper::stripos($ext, $badExt) !== false)
            return false;
    }
    return true;
}
with:
function validExtension($ext) 
{
    if ($this->allowedExts != null) {
        foreach($this->allowedExts as $goodExt) {
            if (FfHelper::stripos($ext, $goodExt) !== false)
                return true;
        }
        return false;
    }
    foreach($this->prohibitedExts as $badExt) {
        if (FfHelper::stripos($ext, $badExt) !== false)
            return false;
    }
    return true;
}
b. Generate a form with FORMgen that has an UploadField called "upload"
c. To only allow the uploading of ".doc" and ".txt" files, edit your form and replace:
$ffForm->addField(new UploadField("upload", "Upload", FORM_FIELD_REQUIRED, 2000000));
with:
$ffForm->addField(new UploadField("upload", "Upload", FORM_FIELD_REQUIRED, 2000000));
$ffForm->formFields["upload"]->allowedExts = array("txt", "doc");
Info
Support | Earn Money
Copyright © 2005-2019 Brain Book Software LLC.
Built with FORMfields, the premiere web framework.