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 Inputfield 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 Collapse all API reference
| Property | Type | Default | Description |
|---|---|---|---|
extensions | string | '' | Space-separated list of allowed file extensions, such as 'pdf jpg png'. |
okExtensions | array | [] | Extensions manually whitelisted even if otherwise flagged as problematic. Added in 3.0.167. |
maxFiles | int | 0 | Maximum files allowed. 0 means no limit; 1 enables single-file replacement behavior. |
maxFilesize | int | PHP limit | Maximum file size in bytes. Accepts shorthand strings through setMaxFilesize(), and is capped to PHP upload_max_filesize. |
useTags | bool|int | 0 | Enable tags per file. 0 off, 1 freeform tags, 8 predefined tags, 9 predefined plus freeform. |
tagsList | string | '' | Space-separated predefined tags used when useTags >= 8. |
unzip | bool|int | 0 | Decompress uploaded ZIP archives and add contained files. Only functional when maxFiles=0. |
overwrite | bool|int | 0 | Replace existing files with the same name. AJAX uploads in overwrite mode are saved immediately. |
descriptionRows | int | 1 | Rows for the description input. 0 disables descriptions; values greater than 1 render a textarea. |
destinationPath | string | '' | Destination disk path for uploads. Usually supplied automatically by FieldtypeFile. |
itemClass | string | auto | CSS classes applied to each rendered file item <li>. |
noUpload | bool|int | 0 | Set to 1 to disable uploading and render only existing file data. |
noLang | bool|int | 0 | Disable multi-language descriptions when Language Support is installed. |
noAjax | bool|int | 0 | Disable AJAX drag-and-drop uploading. |
uploadOnlyMode | int | 0 | Upload-only behavior from request state. 1 hides existing list; 2 also prevents temp status. |
noCollapseItem | bool|int | 0 | Prevent individual file items from collapsing. |
noShortName | bool|int | 0 | Display full basenames rather than shortened names. |
noCustomButton | bool|int | false | Use the browser-native file input instead of ProcessWire's styled button. |
value | Pagefiles|Pagefile|null | null | Current file value. Usually a Pagefiles collection; single-output field contexts can use a Pagefile. |
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);| Hook | When | Arguments |
|---|---|---|
Inputfield | Rendering one file item. | $pagefile, $id, $n |
Inputfield | Rendering the list of files. | $value |
Inputfield | Rendering the upload area. | $value |
Inputfield | After a file is added to the value. | $pagefile |
Inputfield | Extracting metadata before replacement/overwrite. | $pagefile, $metadata |
Inputfield | Adding one uploaded file into the value. | $filename |
Inputfield | Deleting one file item from submitted input. | $pagefile |
Inputfield | Processing description, tags, sort, replace, rename, delete for one item. | $input, $pagefile, $n |
Inputfield | Processing 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';
}
}); - Adding
Inputfieldto an InputfieldForm withFile method=postautomatically sets the formenctypetomultipart/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=1to render without the drag-and-drop target. unzip=1addszipto the allowed extensions only whenmaxFiles=0.maxFiles=1allows 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, oruseTagsNormal | 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/Inputfield
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 Inputfield class also inherits all the methods and properties of: Inputfield, WireData and Wire.
Common
Properties
| Name | Return | Summary | |
|---|---|---|---|
| Inputfield | int | Number of rows for description field or 0 to disable DEFAULT: 1 | |
| Inputfield | string | Destination path for uploaded file or omit for auto. | |
| Inputfield | string | Allowed file extensions, space separated DEFAULT: '' | |
| Inputfield | string | Class name(s) for each file item DEFAULT: auto | |
| Inputfield | int | Maximum number of files allowed or 0 for no max DEFAULT: 0 | |
| Inputfield | int | Maximum file size or 0 for auto detect DEFAULT: 0 | |
| Inputfield | bool int | Set to true or 1 to disable ajax uploading DEFAULT: 0 | |
| Inputfield | bool int | Set to true to disable collapsed items (like for LanguageTranslator tool or other things that add tools to files) DEFAULT: false | |
| Inputfield | bool int | Set to true to disable use of the styled <input type='file'> DEFAULT: false | |
| Inputfield | bool int | Set to true or 1 to disable multi-language descriptions DEFAULT: 0 | |
| Inputfield | bool int | Set to true to disable shortened filenames in output DEFAULT: false | |
| Inputfield | bool int | Set to true or 1 to disable uploading to this field DEFAULT: 0 | |
| Inputfield | array | File extensions that are whitelisted if any in $extensions are problematic. 3.0.167+ DEFAULT: [] | |
| Inputfield | bool int | Whether or not overwrite mode is enabled DEFAULT: false | |
| Inputfield | string | Predefined tags DEFAULT: '' | |
| Inputfield | bool int | Whether or not unzip is enabled DEFAULT: false | |
| Inputfield | int | Set to true or 1 to disable existing file list display, or 2 to also prevent file from having 'temp' status. DEFAULT: 0 | |
| Inputfield | bool | Whether or not tags are enabled DEFAULT: false | |
| Inputfield | Pagefiles Pagefile null | Value attribute |
Additional methods and properties
In addition to the methods and properties above, Inputfield
API reference based on ProcessWire core version 3.0.269