public function onCheckFileAllowed(string $fileJModel $model) : bool

Fires while checking whether the file to upload/delete is allowed.


Description

Trigger hook to allow the plugins to extend the validation of a specific file, in order to allow the upload of file types that are not supported by default or to allow the cancellation of specific files.

NOTE: the system checks whether the specified type of the file to upload is contained within the default list of allowed extensions, which are grouped in categories. Here's all the supported categories:

  • image - for images and pictures (such as .png);
  • video - for multimedia files (such as .mp4);
  • audio - for sounds and audio files (such as .mp3);
  • archive - for compressed archives (such as .zip);
  • document - for textual documents (such as .doc);
  • spreadsheet - for spreadsheet documents (such as .xls);
  • presentation - for presentation documents (such as .pps);
  • text - for plain text documents (such as .txt).

It is possible to support new categories by using the code below:

if (!array_key_exists('coding', $model->allowedFiles))
{
    // create new category
    $model->allowedFiles['coding'] = [];
}

// append new supported types
$model->allowedFiles['coding'][] = 'php';
$model->allowedFiles['coding'][] = 'xml';
$model->allowedFiles['coding'][] = 'html';
$model->allowedFiles['coding'][] = 'js';
$model->allowedFiles['coding'][] = 'css';

The supported extensions are REGEX compliant. The following example adds support for both CSS and SCSS at once. 

$model->allowedFiles['coding'][] = 's?css';

It is also possible to allow the upload with a simple IF statement.

if (preg_match("/\.webp/i", $file))
{
    return true;
}

NOTE: when it is needed to support new file types, it is appropriate to register them also within the onBeforeSaveMedia hook.


Parameters

$file

(string)  The file path/name to check.

$model

(JModel)  The model instance that handles the saving process.

Return Value

Boolean. True to always allow the file upload, false to rely on the default algorithm.


Example

The following example explains how to support new file extensions and how to drop support for a specific category.

/**
 * Trigger hook to allow the plugins to extend the validation of
 * a specific file, in order to support the upload of file types
 * that are not supported by default.
 *
 * @param   string   $file   The file name/path to check.
 * @param   JModel   $model  The model instance.
 *
 * @return  boolean  True to allow the file upload.
 */
public function onCheckFileAllowed($file, $model)
{
    // add support for WebP extension
    $model->allowedFiles['image'] = 'webp';

    // drop support for audio files
    unset($model->allowedFiles['audio']);

    // do not return anything to allow the system to use
    // the default validation method
}

Changelog

VersionDescription
1.7 Introduced.
Last Update: 2021-10-08 13:45
Helpful?