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 Collapse all API reference
| Property | Type | Description |
|---|---|---|
path | string | Full server disk path to the directory that stores the files. |
url | string | Web-accessible URL to the directory that stores the files. |
page | Page | The Page object that owns this file collection. |
field | Field | The Field object that defines this file collection. |
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']; // equivalentfindTag($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');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:
| Option | Type | Default | Description |
|---|---|---|---|
action | string | 'after' | Where to insert: 'append', 'prepend', 'after', 'before', or blank to return without inserting. |
pagefiles | Pagefiles | $this | Target Pagefiles instance for the duplicate. |
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();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();Pagefiles provides two hookable mutation methods:
| Hook | When | Arguments |
|---|---|---|
Pagefiles::delete | Before a file is queued for deletion | Pagefile $file |
Pagefiles::clone | Before a file is duplicated | Pagefile $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.
Pagefilesextends WireArray, so all filtering, sorting, iteration, and array-access features fromWireArrayare 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
Pagefilethat belongs to another page copies the file into the current page's files directory. - For single-file fields, FieldtypeFile may return a
Pagefileobject directly when output formatting is on. - Source file:
wire/core/Pagefiles/Pagefiles.php
API reference: methods, properties, hooks
Pagefile objects are contained by a Pagefiles object.
Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the Pagefile class also inherits all the methods and properties of: WireData and Wire.
Common
| Name | Return | Summary | |
|---|---|---|---|
Pagefile::__construct() Pagefile::__construct(Pagefiles $pagefiles, string $filename) Pagefile::__construct(Pagefiles $pagefiles, string $filename) | None | Construct a new Pagefile | |
Pagefile::basename() Pagefile::basename() Pagefile::basename(bool $ext = true) | string | Returns the basename of this Pagefile (name and extension, without disk path). Can also be used as property: Pagefile::basename | |
Pagefile::description() Pagefile::description() Pagefile::description($language = null, $value = null) | string array | Get or set the file’s description (with multi-language support). Can also be used as property: Pagefile::description | |
Pagefile::ext() Pagefile::ext() Pagefile::ext() | string | Returns the file’s extension - "pdf", "jpg", etc. Can also be used as property: Pagefile::ext | |
Pagefile::filemtime() Pagefile::filemtime() Pagefile::filemtime(bool $reset = false) | int | Get last modified time of file | |
Pagefile::filesize() Pagefile::filesize() Pagefile::filesize(bool $reset = false) | int | Returns the filesize in number of bytes. Can also be used as property: Pagefile::filesize | |
Pagefile::filesizeStr() Pagefile::filesizeStr() Pagefile::filesizeStr() | string | Returns the filesize in a formatted, output-ready string (i.e. "123 kB") Can also be used as property: Pagefile::filesizeStr | |
Pagefile::getFiles() Pagefile::getFiles() Pagefile::getFiles() | array | Get all filenames associated with this file | |
Pagefile::hash() Pagefile::hash() Pagefile::hash() | string | Return a unique MD5 hash representing this Pagefile. Can also be used as property: Pagefile::hash | |
Pagefile::hidden() Pagefile::hidden() Pagefile::hidden($set = null) | None | Get or set hidden state of this file | |
Pagefile::httpUrl() Pagefile::httpUrl() Pagefile::httpUrl() | string | Return the web accessible URL (with scheme and hostname) to this Pagefile. Can also be used as property: Pagefile::httpUrl | |
Pagefile::isNew() Pagefile::isNew() Pagefile::isNew($set = null) | bool | Get or set “new” status of the Pagefile | |
Pagefile::noCacheURL() Pagefile::noCacheURL() Pagefile::noCacheURL($http = false) | string | ||
Pagefile::save() Pagefile::save() Pagefile::save() | bool | Save this Pagefile independently of the Page it lives on | |
Pagefile::uploadName() Pagefile::uploadName() Pagefile::uploadName() | string | Original and unsanitized filename at the time it was uploaded Can also be used as property: Pagefile::uploadName |
Traversal
For the most part you’ll want to traverse from the parent Pagefiles object than these methods.
| Name | Return | Summary | |
|---|---|---|---|
Pagefile::getNext() Pagefile::getNext() Pagefile::getNext() | Pagefile null | Return the next sibling Pagefile in the parent Pagefiles, or NULL if at the end. | |
Pagefile::getPrev() Pagefile::getPrev() Pagefile::getPrev() | Pagefile null | Return the previous sibling Pagefile in the parent Pagefiles, or NULL if at the beginning. |
Manipulation
Remember to follow up any manipulations with a $pages->save() call.
| Name | Return | Summary | |
|---|---|---|---|
Pagefile::addTag() Pagefile::addTag($tag) Pagefile::addTag($tag) | $this | Add the given tag to this file’s tags (if not already present) | |
Pagefile::description() Pagefile::description() Pagefile::description($language = null, $value = null) | string array | Get or set the file’s description (with multi-language support). Can also be used as property: Pagefile::description | |
Pagefile::filedata() Pagefile::filedata() Pagefile::filedata($key = '', $value = null) | mixed | Get or set filedata Can also be used as property: Pagefile::filedata | |
Pagefile::removeTag() Pagefile::removeTag(string $tag) Pagefile::removeTag(string $tag) | $this | Remove the given tag from this file’s tags (if present) | |
Pagefile::rename() Pagefile::rename(string $basename) Pagefile::rename(string $basename) | string bool | Rename this file | |
Pagefile::tags() Pagefile::tags() Pagefile::tags($value = null) | string array | Get or set the "tags" property, when in use. Can also be used as property: Pagefile::tags |
Date time
| Name | Return | Summary | |
|---|---|---|---|
| Pagefile::created | int | Unix timestamp of when file was created. | |
| Pagefile::createdStr | string | Readable date/time string of when Pagefile was created | |
| Pagefile::modified | int | Unix timestamp of when Pagefile (file, description or tags) was last modified. | |
| Pagefile::modifiedStr | string | Readable date/time string of when Pagefile was last modified. | |
| Pagefile::mtime | int | Unix timestamp of when file (only) was last modified. | |
| Pagefile::mtimeStr | string | Readable date/time string when file (only) was last modified. |
For hooks
These methods are only useful for hooking and should not be called directly.
| Name | Return | Summary | |
|---|---|---|---|
Pagefile::install() Pagefile::install(string $filename) Pagefile::install(string $filename) | None | Install this Pagefile |
Tags
Be sure to see the Pagefiles::getTag() and Pagesfiles::findTag() methods, which enable you retrieve files by tag.
| Name | Return | Summary | |
|---|---|---|---|
Pagefile::addTag() Pagefile::addTag($tag) Pagefile::addTag($tag) | $this | Add the given tag to this file’s tags (if not already present) | |
Pagefile::hasTag() Pagefile::hasTag($tag) Pagefile::hasTag($tag) | bool string | Does this file have the given tag(s)? | |
Pagefile::removeTag() Pagefile::removeTag(string $tag) Pagefile::removeTag(string $tag) | $this | Remove the given tag from this file’s tags (if present) | |
Pagefile::tags() Pagefile::tags() Pagefile::tags($value = null) | string array | Get or set the "tags" property, when in use. Can also be used as property: Pagefile::tags | |
| Pagefile::tagsArray | array | Get file tags as an array. |
Other
| Name | Return | Summary | |
|---|---|---|---|
| Pagefile::HTTPURL | string | Same as the cache-busting uppercase “URL” property, but includes scheme and hostname. | |
| Pagefile::URL | string | Same as $url property but with browser cache busting query string appended. | |
| Pagefile::createdUser | User NullPage | User that added/uploaded the file or NullPage if not known 3.0.154+. | |
| Pagefile::created_users_id | int | ID of user that added/uploaded the file or 0 if not known 3.0.154+. | |
| Pagefile::field | Field | The Field object that this file is part of. | |
| Pagefile::modifiedUser | User NullPage | User that last modified the file or NullPage if not known 3.0.154+. | |
| Pagefile::modified_users_id | int | ID of user that last modified the file or 0 if not known 3.0.154+. | |
| Pagefile::page | Page | The Page object that this file is part of. | |
| Pagefile::pagefiles | Pagefiles | The Pagefiles WireArray that contains this file. |
Properties
| Name | Return | Summary | |
|---|---|---|---|
| Pagefile::name | string | Returns the filename without the path, same as the "basename" property. |
Additional methods and properties
In addition to the methods and properties above, Pagefile also inherits the methods and properties of these classes:
API reference based on ProcessWire core version 3.0.269