Files
HTTP Files are encapsulated in 3 classes in Centum:
Centum\Http\Files
implements Centum\Interfaces\Http\FilesInterface
.
Centum\Http\FileGroup
implements Centum\Interfaces\Http\FileGroupInterface
.
Centum\Http\File
implements Centum\Interfaces\Http\FileGroupInte
.
In an effort to simplify file uploads, both from the complexity of HTML file inputs in forms and the complexity of the $_FILES
superglobal, a FileGroup represents all files that share the name
property. In this HTML form, three name
properties exist - images
, documents
, audio
:
<form method="POST" enctype="multipart/form-data">
<input type="file" name="images" multiple>
<input type="file" name="documents[]">
<input type="file" name="documents[]">
<input type="file" name="audio">
<input type="submit" value="Upload">
</form>
Regardless of how files are uploaded and how many are uploaded, they all operate the same way in PHP:
use Centum\Http\FileGroup;
use Centum\Http\File;
use Centum\Http\Files;
/** @var Files $files */
/** @var FileGroup $imagesFileGroup */
$imagesFileGroup = $files->get("images");
foreach ($imagesFileGroup->all() as $image) {
/** @var File $image */
}
/** @var FileGroup $documentsFileGroup */
$documentsFileGroup = $files->get("document");
foreach ($documentsFileGroup->all() as $document) {
/** @var File $document */
}
/** @var FileGroup $audioFileGroup */
$audioFileGroup = $files->get("audio");
foreach ($audioFileGroup->all() as $audio) {
/** @var File $audio */
}
Centum\Http\File
A File object represents an individual file that has been uploaded:
Centum\Http\File(
?string $name,
?string $type,
int $size,
?string $location,
int $error
);
$location
represents the file location on the server.$error
shares the same codes as regular file upload errors.
Files Factory
You can obtain a Files object made with global variables using the FilesFactory:
use Centum\Http\FilesFactory;
$filesFactory = new FilesFactory();
$files = $filesFactory->createFromGlobals();
File Factory
You can obtain a File
object from a real file using the FileFactory
:
use Centum\Http\FileFactory;
$fileFactory = new FileFactory();
$file = $fileFactory->createFromRealFile("/path/to/a/file.txt");