$files->exists() method

Does the given file/link/dir exist?

Thie method accepts an $options argument that can be specified as an array or a string (space or comma separated). The examples here demonstrate usage as a string since it is the simplest for readability.

  • This function may return false for symlinks pointing to non-existing files, unless you specify link as the type.
  • Specifying false for the readable or writable argument disables the option from being used, it doesn’t perform a NOT condition.
  • The writable option may also be written as writeable, if preferred.

Available since version 3.0.180.

Example

// 1. check if exists
$exists = $files->exists('/path/file.ext');

// 2. check if exists and is readable (or writable)
$exists = $files->exists('/path/file.ext', 'readable');
$exists = $files->exists('/path/file.ext', 'writable');

// 3. check if exists and is file, link or dir
$exists = $files->exists('/path/file.ext', 'file');
$exists = $files->exists('/path/file.ext', 'link');
$exists = $files->exists('/path/file.ext', 'dir');

// 4. check if exists and is writable file or dir
$exists = $files->exists('/path/file.ext', 'writable file');
$exists = $files->exists('/path/dir/', 'writable dir');

// 5. check if exists and is readable and writable file
$exists = $files->exists('/path/file.ext', 'readable writable file');

Usage

// basic usage
$bool = $files->exists(string $filename);

// usage with all arguments
$bool = $files->exists(string $filename, $options = '');

Arguments

NameType(s)Description
filenamestring
options (optional)array, string

Can be specified as array or string:

  • type (string): Verify it is of type: 'file', 'link', 'dir' (default='')
  • readable (bool): Verify it is readable? (default=false)
  • writable (bool): Also verify the file is writable? (default=false)
  • writeable (bool): Alias of writable (default=false)
  • When specified as string, you can use any combination of the words: readable, writable, file, link, dir (separated by space or comma).

Return value

bool

Exceptions

Method can throw exceptions on error:

  • WireException - if given invalid or unrecognized $options


$files methods and properties

API reference based on ProcessWire core version 3.0.185

Twitter updates

  • ProcessWire 3.0.192 (dev branch) contains 11 issue fixes and 5 pull requests, over 20 commits— More
    7 January 2022
  • ProcessWire 3.0.191 core updates: updates to the installer and the profile exporter module— More
    24 December 2021
  • This week ProcessWire went on a diet, trimming the core size from 15.5 MB down to 7.5 MB by moving optional site profiles into their own GitHub repos— More
    17 December 2021

Latest news

  • ProcessWire Weekly #401
    In the 401st issue of ProcessWire Weekly we'll cover the latest core updates, check out a brand new module called Lister Native Date Format, and more. Read on!
    Weekly.pw / 15 January 2022
  • ProcessWire 3.0.190 core updates
    ProcessWire 3.0.190 has 15 commits relative to 3.0.189 and contains a mixture of issue resolutions and feature additions. This post covers them all, in addition to updates for the PageAutosave and ProFields Table modules.
    Blog / 3 December 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