file_validate_extensions(FileInterface $file, $extensions)
Checks that the filename ends with an allowed extension.
\Drupal\file\FileInterface $file: A file entity.
string $extensions: A string with a space separated list of allowed extensions.
array An empty array if the file extension is allowed or an array containing an error message if it's not.
function file_validate_extensions(FileInterface $file, $extensions) { $errors = array(); $regex = '/\.(' . preg_replace('/ +/', '|', preg_quote($extensions)) . ')$/i'; if (!preg_match($regex, $file->getFilename())) { $errors[] = t('Only files with the following extensions are allowed: %files-allowed.', array('%files-allowed' => $extensions)); } return $errors; }
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!file!file.module/function/file_validate_extensions/8.1.x