You are here

public function ImceFM::validateFileName in IMCE 8

Same name and namespace in other branches
  1. 8.2 src/ImceFM.php \Drupal\imce\ImceFM::validateFileName()

Validates a file name.

File

src/ImceFM.php, line 577

Class

ImceFM
Imce File Manager.

Namespace

Drupal\imce

Code

public function validateFileName($filename, $silent = FALSE) {

  // Basic validation.
  if ($filename === '.' || $filename === '..' || !($len = strlen($filename)) || $len > 240) {
    return FALSE;
  }

  // Test name filters.
  if ($name_filter = $this
    ->getNameFilter()) {
    if (preg_match($name_filter, $filename)) {
      if (!$silent) {
        $this
          ->setMessage($this
          ->t('%filename is not allowed.', [
          '%filename' => $filename,
        ]));
      }
      return FALSE;
    }
  }

  // Test chars forbidden in various operating systems.
  if (preg_match('@^\\s|\\s$|[/\\\\:\\*\\?"<>\\|\\x00-\\x1F]@', $filename)) {
    if (!$silent) {
      $this
        ->setMessage($this
        ->t('%filename contains invalid characters. Use only alphanumeric characters for better portability.', [
        '%filename' => $filename,
      ]));
    }
    return FALSE;
  }
  return TRUE;
}