$sanitizer->validateFile() method

Validate and sanitize a file using FileValidator modules

This is intended for validating file data, not file names. Depending on the FileValidator modules that are used, they may sanitize the file in order ot make it valid.

IMPORTANT: This method returns NULL if it can’t find a validator for the file. This does not mean the file is invalid, just that it didn't have the tools to validate it. If the getArray option is specified then it would return a blank array rather than null.

getArray option (3.0.167+):
When specifying true for the getArray option this method will return an associative array of validation results indexed by module name. The values for each module name will be either true (file validates as-is), 1 (file valid after it was sanitized), or false (file not valid and cannot be sanitized). A blank array is returned if no modules could perform the validation.

dryrun option (3.0.167+):
When specifying true for the dryrun option please note that no validation is performed and instead the method returns true or false as to whether or not the file can be validated. It only looks at the file extension, so the file need not exist. Meaning it’s also okay to specify filename like “test.jpg” without path, when using this option. If using the dryrun option with the getArray option then it will return an array of module names that would perform the validation for the given file type (or blank array if none).

Usage

// basic usage
$bool = $sanitizer->validateFile(string $filename);

// usage with all arguments
$bool = $sanitizer->validateFile(string $filename, array $options = []);

Arguments

NameType(s)Description
filenamestring

Full path and filename to validate

options (optional)array

When available, provide array with any one or all of the following:

  • page (Page): Page object associated with $filename. (default=null)
  • field (Field): Field object associated with $filename. (default=null)
  • pagefile (Pagefile): Pagefile object associated with $filename. (default=null)
  • getArray (bool): Return array of results rather than a boolean? (default=false) Added 3.0.167
  • dryrun (bool|int): Specify true to only return if the file can be validated with this method, without actually performing any validation. (default=false). Added 3.0.167

Return value

bool array null

Returns one of the following, depending on use of dryrun and getArray options:

  • Boolean true if valid, false if not.
  • NULL if no validator available for given file type or file does not exist.
  • If dryrun option is used, returns boolean (or array of strings if getArray option is true).
  • If getArray option is used, returns associative array of results or blank array if no validators.

$sanitizer methods and properties

API reference based on ProcessWire core version 3.0.184

Twitter updates

  • ProcessWire 3.0.185 (dev) core updates, plus new Session Allow module— More
    17 September 2021
  • Three new ProcessWire Textformatter modules: Find/Replace, Markdown in Markup, and Emoji— More
    3 September 2021
  • This week we have a new master version released after a year in the making. With nearly 40 pull requests, hundreds of new additions and more than 100 issue reports resolved, this new version has a ton of great new stuff— More
    27 August 2021

Latest news

  • ProcessWire Weekly #384
    In the 384th issue of ProcessWire Weekly we'll cover the latest core updates, introduce a new module called Session Allow, and highlight a new site of the week. Read on!
    Weekly.pw / 18 September 2021
  • ProcessWire 3.0.184 new master/main version
    This week we have a new master/main version released after a full year in the making. As you might imagine, this new version has a ton of great new stuff and we’ll try to cover much of it here.
    Blog / 27 August 2021
  • Subscribe to weekly ProcessWire news

“To Drupal, or to ProcessWire? The million dollar choice. We decided to make an early switch to PW. And in retrospect, ProcessWire was probably the best decision we made. Thanks are due to ProcessWire and the amazing system and set of modules that are in place.” —Unni Krishnan, Founder of PigtailPundits