InputfieldFile

One or more file uploads with sortable ordering, inline descriptions, optional tags, drag-and-drop AJAX uploading, and ZIP archive decompression

This is the base Inputfield used by FieldtypeFile and extended by InputfieldImage.

$f = $modules->get('InputfieldFile');
$f->name = 'documents';
$f->label = 'Documents';
$f->extensions = 'pdf doc docx xls xlsx';
$f->maxFiles = 5;
$form->add($f);

When InputfieldFile is produced by a FieldtypeFile field, ProcessWire sets the hasPage, hasField, hasFieldtype, value, destination path hook, and field settings automatically. Standalone use should set at least name, extensions, value/destinationPath when processing uploads, and add the inputfield to an InputfieldForm so the form can receive the upload enctype.

For shared Inputfield API such as attributes, labels, collapsed states, showIf, rendering, processing, errors, and wrapper classes, see Inputfield.

Expand all      API reference

Properties
PropertyTypeDefaultDescription
extensionsstring''Space-separated list of allowed file extensions, such as 'pdf jpg png'.
okExtensionsarray[]Extensions manually whitelisted even if otherwise flagged as problematic. Added in 3.0.167.
maxFilesint0Maximum files allowed. 0 means no limit; 1 enables single-file replacement behavior.
maxFilesizeintPHP limitMaximum file size in bytes. Accepts shorthand strings through setMaxFilesize(), and is capped to PHP upload_max_filesize.
useTagsbool|int0Enable tags per file. 0 off, 1 freeform tags, 8 predefined tags, 9 predefined plus freeform.
tagsListstring''Space-separated predefined tags used when useTags >= 8.
unzipbool|int0Decompress uploaded ZIP archives and add contained files. Only functional when maxFiles=0.
overwritebool|int0Replace existing files with the same name. AJAX uploads in overwrite mode are saved immediately.
descriptionRowsint1Rows for the description input. 0 disables descriptions; values greater than 1 render a textarea.
destinationPathstring''Destination disk path for uploads. Usually supplied automatically by FieldtypeFile.
itemClassstringautoCSS classes applied to each rendered file item <li>.
noUploadbool|int0Set to 1 to disable uploading and render only existing file data.
noLangbool|int0Disable multi-language descriptions when Language Support is installed.
noAjaxbool|int0Disable AJAX drag-and-drop uploading.
uploadOnlyModeint0Upload-only behavior from request state. 1 hides existing list; 2 also prevents temp status.
noCollapseItembool|int0Prevent individual file items from collapsing.
noShortNamebool|int0Display full basenames rather than shortened names.
noCustomButtonbool|intfalseUse the browser-native file input instead of ProcessWire's styled button.
valuePagefiles|Pagefile|nullnullCurrent file value. Usually a Pagefiles collection; single-output field contexts can use a Pagefile.
Methods

getDisplayBasename(Pagefile $pagefile, $maxLength = 25)

Return a basename for display in the file list. Long names are shortened unless noShortName is true.

$name = $f->getDisplayBasename($pagefile);

getWireUpload()

Return the current WireUpload instance, creating it on first call. This is most useful for inspecting upload errors after processing.

$upload = $f->getWireUpload();
foreach($upload->getErrors() as $error) {
    $log->save('uploads', $error);
}

isEmpty()

Return true when the value contains no files.

if($f->isEmpty()) {
    echo "No files uploaded.";
}

getItemInputfields(?Pagefile $item = null)

Return an InputfieldWrapper containing custom fields configured for the Pagefile field's template. Pass a Pagefile to populate values for that item, or omit the argument to prepare those inputs for render-ready state.

$inputfields = $f->getItemInputfields($pagefile);
if($inputfields) echo $inputfields->render();

Returns false when the underlying file field has no custom fields template or when the current value cannot provide one.

setMaxFilesize($filesize)

Set the maximum file size in bytes. Accepts an integer or shorthand strings like '30m', '2g', or '500k'. The stored value is capped to PHP's upload_max_filesize.

$f->setMaxFilesize('50m');
$f->setMaxFilesize(1048576);
Hooks
HookWhenArguments
InputfieldFile::renderItemRendering one file item.$pagefile, $id, $n
InputfieldFile::renderListRendering the list of files.$value
InputfieldFile::renderUploadRendering the upload area.$value
InputfieldFile::fileAddedAfter a file is added to the value.$pagefile
InputfieldFile::extractMetadataExtracting metadata before replacement/overwrite.$pagefile, $metadata
InputfieldFile::processInputAddFileAdding one uploaded file into the value.$filename
InputfieldFile::processInputDeleteFileDeleting one file item from submitted input.$pagefile
InputfieldFile::processInputFileProcessing description, tags, sort, replace, rename, delete for one item.$input, $pagefile, $n
InputfieldFile::processItemInputfieldsProcessing custom fields for one Pagefile item.$pagefile, $inputfields, $id, $input
$wire->addHookAfter('InputfieldFile::fileAdded', function(HookEvent $event) {
    $pagefile = $event->arguments(0); /** @var Pagefile $pagefile */
    if(!$pagefile->description) {
        $name = pathinfo($pagefile->name, PATHINFO_FILENAME);
        $pagefile->description = ucwords(str_replace(['-', '_'], ' ', $name));
    }
});
$wire->addHookBefore('InputfieldFile::processInput', function(HookEvent $event) {
    $inputfield = $event->object; /** @var InputfieldFile $inputfield */
    if($inputfield->name === 'documents') {
        $inputfield->extensions = 'pdf docx';
    }
});
Notes
  • Adding InputfieldFile to an InputfieldForm with method=post automatically sets the form enctype to multipart/form-data.
  • renderUpload() appends [] to the file input name when the name does not already end with ].
  • AJAX uploads are enabled by default. Set noAjax=1 to render without the drag-and-drop target.
  • unzip=1 adds zip to the allowed extensions only when maxFiles=0.
  • maxFiles=1 allows replacement of the current file and sets the upload max to one file.
  • Overwrite mode preserves existing description, tags, and filedata where possible, and refuses overwrites that would replace a file owned by another field on the same page.
  • Tags use FieldtypeFile constants: useTagsOff, useTagsNormal, useTagsPredefined, or useTagsNormal | useTagsPredefined.
  • Pagefile custom fields are configured through the file field's template. Use getItemInputfields() only when the value has a real Page/Field context.
  • InputfieldImage extends this class and adds image-specific rendering, editing, focus point, and variation behavior.

Source file: wire/modules/Inputfield/InputfieldFile/InputfieldFile.module

API reference: methods, properties, hooks

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

Show class?     Show args?       Only hookable?    

Common

NameReturnSummary 
InputfieldFile::get($key)
None 
InputfieldFile::getConfigInputfields()
InputfieldWrapper

Configuration settings for InputfieldFile

InputfieldFile::getDisplayBasename(Pagefile $pagefile)
string

Get a basename for the file, possibly shortened, suitable for display in InputfieldFileList

 
InputfieldFile::getItemInputfields()
bool InputfieldWrapper

Get custom Inputfields for editing given Pagefile

 
InputfieldFile::getWireUpload()
WireUpload

Return the current WireUpload instance or create a new one if not yet created

 
InputfieldFile::isEmpty()
None

Per Inputfield interface, returns true when this field is empty

 
InputfieldFile::processInput(WireInputData $input)
self

Process input

InputfieldFile::render()
string

Render Inputfield input

InputfieldFile::renderReady()
bool

Render ready

 
InputfieldFile::renderValue()
string

Render Inputfield value

InputfieldFile::set($key, $value)
None 
InputfieldFile::setAttribute($key, $value)
Inputfield InputfieldFile

Set an attribute

 
InputfieldFile::setMaxFilesize($filesize)
$this

Set the max file size in bytes or use string like "30m", "2g" "500k"

 
InputfieldFile::setParent(InputfieldWrapper $parent)
$this

Set the parent of this Inputfield

 

Properties

NameReturnSummary 
InputfieldFile::descriptionRows int Number of rows for description field or 0 to disable
DEFAULT: 1
 
InputfieldFile::destinationPath string Destination path for uploaded file or omit for auto. 
InputfieldFile::extensions string Allowed file extensions, space separated
DEFAULT: ''
 
InputfieldFile::itemClass string Class name(s) for each file item
DEFAULT: auto
 
InputfieldFile::maxFiles int Maximum number of files allowed or 0 for no max
DEFAULT: 0
 
InputfieldFile::maxFilesize int Maximum file size or 0 for auto detect
DEFAULT: 0
 
InputfieldFile::noAjax bool int Set to true or 1 to disable ajax uploading
DEFAULT: 0
 
InputfieldFile::noCollapseItem bool int Set to true to disable collapsed items (like for LanguageTranslator tool or other things that add tools to files)
DEFAULT: false
 
InputfieldFile::noCustomButton bool int Set to true to disable use of the styled <input type='file'>
DEFAULT: false
 
InputfieldFile::noLang bool int Set to true or 1 to disable multi-language descriptions
DEFAULT: 0
 
InputfieldFile::noShortName bool int Set to true to disable shortened filenames in output
DEFAULT: false
 
InputfieldFile::noUpload bool int Set to true or 1 to disable uploading to this field
DEFAULT: 0
 
InputfieldFile::okExtensions array File extensions that are whitelisted if any in $extensions are problematic. 3.0.167+
DEFAULT: []
 
InputfieldFile::overwrite bool int Whether or not overwrite mode is enabled
DEFAULT: false
 
InputfieldFile::tagsList string Predefined tags
DEFAULT: ''
 
InputfieldFile::unzip bool int Whether or not unzip is enabled
DEFAULT: false
 
InputfieldFile::uploadOnlyMode int Set to true or 1 to disable existing file list display, or 2 to also prevent file from having 'temp' status.
DEFAULT: 0
 
InputfieldFile::useTags bool Whether or not tags are enabled
DEFAULT: false
 
InputfieldFile::value Pagefiles Pagefile null Value attribute  

Additional methods and properties

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

API reference based on ProcessWire core version 3.0.269