Download the PHP package tvanc/files-array-organizer without Composer
On this page you can find all versions of the php package tvanc/files-array-organizer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tvanc/files-array-organizer
More information about tvanc/files-array-organizer
Files in tvanc/files-array-organizer
Package files-array-organizer
Short Description Creates an organized version of the $_FILES array.
License mit
Informations about the package files-array-organizer
Files Array Organizer
Getting Started | Examples | Explanation |
---|
Dealing with the $_FILES
array in PHP sucks. Most solutions only work for a
specific situation and aren't easily portable. This utility is designed to organize any
possible incarnation of the $_FILES
array into the structure you would intuitively expect. Getting data about uploaded files should be just as easy as reading the $_POST
array.
Getting Started
Requirements
PHP >= 7.3
Installation
Examples
One input, one file
In a case like this, the $_FILES
array is pretty straight forward and the organized version is identical to the unorganized version.
A single input that accepts multiple files
FilesArrayOrganizer
makes dealing with this common scenario a little easier.
Multiple inputs that accept multiple files
FilesArrayOrganizer
makes dealing with this less-common scenario much easier. Check the Explanation to see the surprising $_FILES
array you can get from a form containing multiple inputs that each accept multiple files.
Execute a custom callback on each file
To alter how file data is represented, pass a callback as the second argument to FilesArrayOrganizer::organize()
. The callback receives one argument, which is an array of file data. The callback's return value will be used to represent the file instead of the received array parameter.
Explanation
For some situations, dealing with the $_FILES
superglobal is fine. Uploading a single file via a field named attachment
would generate a simple $_FILES
array like this:
Getting the file's attributes is easy. The path to the input's value corresponds to the name of the input field.
What about a field named todo[0][attachments][]
, which accepts multiple files? Working with the files from this field should be easy. Right?
Wrong.
You thought the $_FILES
array would look like this.
It will actually look like this:
I leave it as an exercise for the reader to figure out how to extract the useful information out of that. If you don't want to repeat that exercise every time you need to handle more than one file at a time, just use this library.
To see how to use FilesArrayOrganizer
with a $_FILES
array just like this, see the example,
Multiple inputs that accept multiple files.