Luminova Framework

PHP Luminova: HTTP Request Upload File Object

Last updated: 2024-10-04 09:06:15

File object encapsulates a file ready to be uploaded to a server. It offers customization options before sending the file to the upload class, It allows you to modify properties like filename, etc...

The File object represents a file ready to be uploaded to the server. With this class, you can customize files before sending them to the Uploader or Storage class for final upload. Optionally, you can change the filename or set a symlink path to generate a symbolic link for files stored in the private storage directory.


Configuration Example

Setting upload configurations for the file object

<?php
use Luminova\Http\Request;

$request = new Request();
$file = $request->getFile('image');

// Set various upload configurations
$file->setConfig([
    'upload_path'   => root('writeable/storages/uploads/'),
    'max_size'      => 10485760,
    'min_size'      => 1024,
    'allowed_types' => 'png|jpg|gif|pdf',
    'chunk_length'  => 5242880,
    'if_existed'    => File::IF_EXIST_RETAIN,
    'symlink'       => '/public/assets/symlink',
]);

// Validate the file before proceeding with upload
if ($file->valid()) {
    echo 'File is valid for upload';
} else {
    echo 'File validation failed: (' . $file->getError() . ') ' . $file->getMessage();
}

Explanation:

  1. upload_path: Specifies the writeable/storages/uploads/ as the path where the file will be saved on the server.
  2. max_size: Defines the maximum allowable file size. In this example, 10MB is the maximum size.
  3. min_size: Sets the minimum allowable size for the file, such as 1KB in the example.
  4. allowed_types: Restricts file types to specific extensions (png, jpg, gif, pdf).
  5. chunk_length: Enables chunked file uploads, useful for large files (5MB chunks in this case).
  6. if_existed: Determines how to handle existing files; File::IF_EXIST_RETAIN retains old versions of files.
  7. symlink: Creates a symlink in the specified path /public/assets/symlink once the upload completes.

Class Definition

  • Class namespace: \Luminova\Http\File

Constants

These constant are available for custom error code and custom file configurations for upload behavior.

ConstantTypeValueDescription
UPLOAD_ERR_NO_SIZEint9Custom upload error: file has no size.
UPLOAD_ERR_MIN_SIZEint10Custom upload error: file is below the minimum allowed size.
IF_EXIST_RETAINstringretainsetConfig possible value for key if_existed: Retain the old version and create a new version of the file if it exists.
IF_EXIST_OVERWRITEstringoverwritesetConfig possible value for key if_existed: Overwrite the existing file if it exists.

Methods

constructor

Constructs a File object.

public __construct(
    protected int $index = 0,
    protected ?string $name = null,
    protected ?string $type = null,
    protected int $size = 0,
    protected ?string $extension = null,
    protected ?string $temp = null,
    protected int $error = UPLOAD_ERR_NO_FILE,
    protected ?string $content = null
)

Parameters:

ParameterTypeDescription
$indexintOptional index of the file (default: 0).
$namestring|nullThe name of the file (default: null).
$typestring|nullThe MIME type of the file (e.g, image/jpeg).
$sizeintThe uploaded file size in bytes (default: 0).
$extensionstring|nullThe uploaded file extension type (e.g, png, pdf).
$tempstring|nullThe uploaded file temporary filepath (default: null).
$errorintThe error code UPLOAD_ERR_* of the uploaded file (default: UPLOAD_ERR_NO_FILE).
$contentstring|nullThe file string content, may be available if temp_name is not.

getters

Magic getter method to allow access to protected properties.

<?php 
echo $file->name; // File name
echo $file->type; // File MIME type
echo $file->size; // File size
echo $file->extension; // File extension type 
echo $file->temp; // File temp filepath
echo $file->error; // File error code
echo $file->message; // File validation message
echo $file->content; // File string content 
echo $file->index; // File index 

getIndex

Gets the index of the file.

public getIndex(): int

Return Value:

int - Return the index of the file.


getName

Gets the name of the file.

public getName(): ?string

Return Value:

string|null - Return the name of the file.


getType

Gets the MIME type of the file.

public getType(): ?string

Return Value:

string|null - Return the MIME type of the file.


getMime

Gets the MIME type of the file.

public getMime(): ?string

Return Value:

string|null - Return the MIME type of the file.

Alias of getType.


getSize

Gets the size of the file in bytes.

public getSize(): int

Return Value:

int - Return the size of the file in bytes.


getExtension

Gets the file extension.

public getExtension(): ?string

Return Value:

string|null - Return the file extension.


getTemp

Gets the temporary file path.

public getTemp(): ?string

Return Value:

string|null - Return the temporary file path.


getError

Gets the error code of the file upload.

public getError(): int

Return Value:

int - Return the error code of the file upload.


getMessage

Gets the validation message.

public getMessage(): ?string

Return Value:

string|null - Return the validation message.


getConfig

Gets file upload configurations.

public getConfig(): ?\stdClass

Return Value:

\stdClass|null - Return upload configurations.


setName

This method allows you to change the file name before finally uploading.

public setName(string $name): self

Parameters:

ParameterTypeDescription
$namestringThe name of the file.

Return Value:

self - Return instance of file object.

Throws:


setConfig

Set file configurations for upload behavior.

public setConfig(array<string,string|int> $config): self

Parameters:

ParameterTypeDescription
$configarray<string,string|int>An associative array of file configuration key and value.

Return Value:

self - Return instance of file object.

Note: The configurations will be used in validating file before uploading to server.

Supported Configurations Keys

  • upload_path - (string) The path where files will be uploaded.
  • max_size - (int) Maximum allowed file size in bytes.
  • min_size - (int) Minimum allowed file size in bytes.
  • allowed_types - (string|string[]) Array of allowed file types or String separated by pipe symbol (e.g, png|jpg|gif).
  • chunk_length - (int) Length of chunk in bytes (default: 5242880).
  • if_existed - (string) How to handle existing files [File::IF_EXIST_OVERWRITE or File::IF_EXIST_RETAIN] (default: File::IF_EXIST_OVERWRITE).
  • symlink - (string) Specify a valid path to create a symlink after upload was completed (e.g /public/assets/).

free

Reset file configuration and remove temp file.

public free(): void

valid

Validates file to determine if it passed all basic and the specified configuration rules.

public valid(): bool

Return Value:

bool - Return true if the file validations passed, false otherwise.