Pagefiles

A Pagefiles object is a WireArray collection of Pagefile objects

It is the value type for multi-file fields in ProcessWire (created by FieldtypeFile) and provides methods for adding, removing, renaming, duplicating, and finding files by tag or basename.

// $page->files is a Pagefiles instance
foreach($page->files as $name => $file) {
    echo "<li><a href=\'$file->url\'>$name</a> — $file->description</li>";
}

Expand all      API reference

Properties
PropertyTypeDescription
pathstringFull server disk path to the directory that stores the files.
urlstringWeb-accessible URL to the directory that stores the files.
pagePageThe Page object that owns this file collection.
fieldFieldThe Field object that defines this file collection.
Retrieval

Pagefiles extends WireArray, so all traversal, filtering, slicing, sorting, and counting methods from WireArray are available. The methods below are specific to file collections.

getFile($name)

Get a Pagefile by its basename. Returns null if not found. Array access with the basename is equivalent.

$file = $page->files->getFile('document.pdf');
$file = $page->files['document.pdf']; // equivalent

findTag($tag)

Return a new Pagefiles collection containing only files that match the given tag expression.

// Single tag
$downloads = $page->files->findTag('download');

// OR: files with any of these tags
$featured = $page->files->findTag('featured|hero|gallery');

// AND: files with all of these tags
$hero = $page->files->findTag('featured,hero');

getTag($tag)

Return the first Pagefile matching the given tag expression, or null if none match. Accepts the same tag syntax as findTag().

$hero = $page->files->getTag('hero');

tags($value)

When called without arguments, returns all tags used by files in this collection as a space-separated string. Pass true to receive an associative array. Pass a tag string to retrieve files matching those tags (alias of findTag()).

$allTags = $page->files->tags();       // "download featured pdf"
$tagsArray = $page->files->tags(true); // ['download' => 'download', ...]

$downloads = $page->files->tags('download');
Manipulation

Destructive changes are queued until the owning page is saved. Call $page->save('field_name') to persist additions, deletions, renames, and duplications.

add($item)

Add a new file to the collection. Accepts a local path, an HTTP(S) URL, or an existing Pagefile object. The file is copied into the page's files directory and given a sanitized, unique basename.

$page->files->add('/path/to/document.pdf');
$page->files->add('https://example.com/image.png');
$page->save('files');

When adding a Pagefile that belongs to a different page, the file is copied into the current page's files directory and marked as temporary until saved.

delete($item)

Queue a Pagefile (or its basename) for deletion. The file is removed from the collection immediately and deleted from disk when the page is saved.

$page->files->delete('old-document.pdf');
$page->save('files');

deleteAll()

Queue every file in the collection for deletion.

$page->files->deleteAll();
$page->save('files');

rename($item, $name)

Queue a rename of a Pagefile. The actual rename occurs when the page is saved.

$file = $page->files->getFile('document.pdf');
$page->files->rename($file, 'report.pdf');
$page->save('files');

clone($item, $options)

Duplicate a Pagefile and insert it into the collection. Returns the new Pagefile or false on failure. The duplicated file is temporary until the page is saved.

$file = $page->files->getFile('document.pdf');
$copy = $page->files->clone($file);
$copy->description = 'Copy of document.pdf';
$page->save('files');

Available options:

OptionTypeDefaultDescription
actionstring'after'Where to insert: 'append', 'prepend', 'after', 'before', or blank to return without inserting.
pagefilesPagefiles$thisTarget Pagefiles instance for the duplicate.
Paths

path()

Return the full disk path where the files are stored.

$path = $page->files->path();

url()

Return the web-accessible URL where the files are stored.

$url = $page->files->url();
File metadata

cleanBasename($basename, $originalize = false, $allowDots = true, $translate = false)

Sanitize a filename for use in this collection. Usually called internally when files are added, but available for custom file handling.

$safe = $page->files->cleanBasename('My File.PDF', true);

getFiles()

Return a flat array of disk paths for every file in the collection, including any extra files managed by PagefileExtra.

$paths = $page->files->getFiles();
Hooks

Pagefiles provides two hookable mutation methods:

HookWhenArguments
Pagefiles::deleteBefore a file is queued for deletionPagefile $file
Pagefiles::cloneBefore a file is duplicatedPagefile $item, array $options
$wire->addHookBefore('Pagefiles::delete', function(HookEvent $event) {
    $pagefiles = $event->object; // Pagefiles
    $file = $event->arguments(0); // Pagefile
    // log deletion, check permissions, etc.
});

Individual file URLs and filenames can be hooked on the Pagefile class. See Pagefile for details.

Notes
  • Pagefiles extends WireArray, so all filtering, sorting, iteration, and array-access features from WireArray are available.
  • Items are keyed by basename, so $page->files['report.pdf'] works.
  • Files are not actually deleted, renamed, or committed until the owning page is saved.
  • Adding a Pagefile that belongs to another page copies the file into the current page's files directory.
  • For single-file fields, FieldtypeFile may return a Pagefile object directly when output formatting is on.
  • Source file: wire/core/Pagefiles/Pagefiles.php

API reference: methods, properties, hooks

The items in a Pagefiles array are Pagefile objects, indexed by file basename, i.e. myfile.pdf. Information on most traversal, filtering and manipulation methods can be found in the WireArray class that Pagefiles extends. In the examples below, $page->files is an instance of Pagefiles:

// Determining if any files are present
if($page->files->count()) {
  // There are files here
}

// Traversing and outputting links to all files
foreach($page->files as $name => $pagefile) {
  echo "<li><a href='$pagefile->url'>$name: $pagefile->description</a></li>";
}

// Adding new file(s)
$page->files->add('/path/to/file.pdf');
$page->files->add('http://domain.com/photo.png');
$page->save('files');

// Getting file by name
$pagefile = $page->files->getFile('file.pdf');
$pagefile = $page->files['file.pdf']; // alternate

// Getting first and last file
$pagefile = $page->files->first();
$pagefile = $page->files->last();

Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the Pagefiles class also inherits all the methods and properties of: WireArray and Wire.

Show class?     Show args?       Only hookable?    

Common

NameReturnSummary 
Pagefiles::clone(Pagefile $item)
Pagefile bool

Duplicate the Pagefile and add to this Pagefiles instance

Pagefiles::formatted()
bool

Get or set formatted state

 
Pagefiles::getField()
Field null

Get the field these files are assigned to

 
Pagefiles::getFieldsPage()
Page

Get mock/placeholder Page object used for Pagefile custom fields

 
Pagefiles::getFile(string $name)
null Pagefile

Get the Pagefile having the given basename, or null if not found.

 
Pagefiles::getFiles()
array

Get all filenames associated with this Pagefiles object

 
Pagefiles::getPage()
Page

Get the page these files are assigned to

 
Pagefiles::path()
string

Return the full disk path where files are stored


Can also be used as property: Pagefiles::path
 
Pagefiles::setField(Field $field)
None

Set the field these files are assigned to

 
Pagefiles::setPage(Page $page)
None

Set the Page these files are assigned to

 
Pagefiles::url()
string

Returns the web accessible index URL where files are stored


Can also be used as property: Pagefiles::url
 

Manipulation

NameReturnSummary 
Pagefiles::add($item)
$this

Add a new Pagefile item or filename

 
Pagefiles::delete($item)
$this

Delete a pagefile item

Pagefiles::deleteAll()
$this

Delete all files associated with this Pagefiles instance, leaving a blank Pagefiles instance.

 
Pagefiles::rename(Pagefile $item, string $name)
Pagefiles

Queue a rename of a Pagefile

 

Tags

NameReturnSummary 
Pagefiles::findTag($tag)
Pagefiles

Return all Pagefile objects that have the given tag(s).

 
Pagefiles::getTag(string $tag)
Pagefile null

Return the first Pagefile that matches the given tag or NULL if no match

 
Pagefiles::tags()
string array Pagefiles

Get list of tags for all files in this Pagefiles array, or return files matching given tag(s)

 

Other

NameReturnSummary 
Pagefiles::field Field Returns the Field that contains this set of files, same as the getField() method.  
Pagefiles::page Page Returns the Page that contains this set of files, same as the getPage() method.  

Additional methods and properties

In addition to the methods and properties above, Pagefiles also inherits the methods and properties of these classes:

API reference based on ProcessWire core version 3.0.269