Files

The Files class provides a simple and secure way to handle file uploads and manage stored files.

Constructor

__construct
__construct(string $storagePath, array $config = []): Files

Creates a new Files instance with the specified storage path and configuration.

Parameters

NameTypeRequiredDescription
$storagePath string Yes Base directory for file storage
$config array No Configuration options for file handling

Example

$config = [
    'db' => [
        'dsn' => 'mysql:host=localhost;dbname=schoolkit;charset=utf8mb4',
        'user' => 'your_username',
        'pass' => 'your_password'
    ],
    'storage_path' => __DIR__ . '/storage'
];

$schoolKit = SchoolKit::start($config);
$files = $schoolKit->files();

// Configure allowed file types and size limits
$files->setAllowedMimeTypes(['image/jpeg', 'image/png']);
$files->setMaxFileSize(2097152); // 2MB

Methods

store
store(array $uploadedFile, array $options = []): array

Stores an uploaded file.

Parameters

NameTypeRequiredDescription
$uploadedFile array Yes The file array from $_FILES
$options array No Options like directory, preserve_name, allowed_mime_types, and max_file_size

Returns

An array containing information about the stored file.

Example

$config = [
    'db' => [
        'dsn' => 'mysql:host=localhost;dbname=schoolkit;charset=utf8mb4',
        'user' => 'your_username',
        'pass' => 'your_password'
    ],
    'storage_path' => __DIR__ . '/storage'
];

$schoolKit = SchoolKit::start($config);
$files = $schoolKit->files();

if (!empty($_FILES['avatar'])) {
    $result = $files->store($_FILES['avatar'], ['directory' => 'avatars']);
    // $result = ['path' => 'avatars/...', ...]
}
delete
delete(string $path): bool

Deletes a file from storage.

Parameters

NameTypeRequiredDescription
$path string Yes The path to the file relative to the storage directory

Returns

true on success, false if the file doesn't exist.

Example

$config = [
    'db' => [
        'dsn' => 'mysql:host=localhost;dbname=schoolkit;charset=utf8mb4',
        'user' => 'your_username',
        'pass' => 'your_password'
    ],
    'storage_path' => __DIR__ . '/storage'
];

$schoolKit = SchoolKit::start($config);
$files = $schoolKit->files();
$success = $files->delete('avatars/some_file.jpg');
exists
exists(string $path): bool

Checks if a file exists in storage.

Parameters

NameTypeRequiredDescription
$path string Yes The path to the file relative to the storage directory

Returns

true if the file exists, false otherwise.

Example

$config = [
    'db' => [
        'dsn' => 'mysql:host=localhost;dbname=schoolkit;charset=utf8mb4',
        'user' => 'your_username',
        'pass' => 'your_password'
    ],
    'storage_path' => __DIR__ . '/storage'
];

$schoolKit = SchoolKit::start($config);
$files = $schoolKit->files();
$exists = $files->exists('avatars/some_file.jpg');
getFullPath
getFullPath(string $path): string

Gets the full absolute path to a file in storage.

Parameters

NameTypeRequiredDescription
$path string Yes The path to the file relative to the storage directory

Returns

The absolute path to the file.

Example

$config = [
    'db' => [
        'dsn' => 'mysql:host=localhost;dbname=schoolkit;charset=utf8mb4',
        'user' => 'your_username',
        'pass' => 'your_password'
    ],
    'storage_path' => __DIR__ . '/storage'
];

$schoolKit = SchoolKit::start($config);
$files = $schoolKit->files();
$fullPath = $files->getFullPath('avatars/some_file.jpg');
// $fullPath = '/path/to/storage/avatars/some_file.jpg'
getFileInfo
getFileInfo(string $path): array

Gets information about a file in storage.

Parameters

NameTypeRequiredDescription
$path string Yes The path to the file relative to the storage directory

Returns

An array containing file information (size, MIME type, etc.).

Example

$config = [
    'db' => [
        'dsn' => 'mysql:host=localhost;dbname=schoolkit;charset=utf8mb4',
        'user' => 'your_username',
        'pass' => 'your_password'
    ],
    'storage_path' => __DIR__ . '/storage'
];

$schoolKit = SchoolKit::start($config);
$files = $schoolKit->files();
$info = $files->getFileInfo('avatars/some_file.jpg');
createDirectory
createDirectory(string $path): bool

Creates a new directory within the storage path.

Parameters

NameTypeRequiredDescription
$path string Yes The path of the directory to create, relative to the storage path

Returns

true on success, false otherwise.

Example

$config = [
    'db' => [
        'dsn' => 'mysql:host=localhost;dbname=schoolkit;charset=utf8mb4',
        'user' => 'your_username',
        'pass' => 'your_password'
    ],
    'storage_path' => __DIR__ . '/storage'
];

$schoolKit = SchoolKit::start($config);
$files = $schoolKit->files();
$success = $files->createDirectory('new_folder');
setAllowedMimeTypes
setAllowedMimeTypes(array $mimeTypes): void

Sets the allowed MIME types for file uploads.

Parameters

NameTypeRequiredDescription
$mimeTypes array Yes An array of allowed MIME types

Example

$config = [
    'db' => [
        'dsn' => 'mysql:host=localhost;dbname=schoolkit;charset=utf8mb4',
        'user' => 'your_username',
        'pass' => 'your_password'
    ],
    'storage_path' => __DIR__ . '/storage'
];

$schoolKit = SchoolKit::start($config);
$files = $schoolKit->files();
$files->setAllowedMimeTypes(['image/jpeg', 'image/png']);
setMaxFileSize
setMaxFileSize(int $bytes): void

Sets the maximum allowed file size for uploads.

Parameters

NameTypeRequiredDescription
$bytes int Yes The maximum file size in bytes

Example

$config = [
    'db' => [
        'dsn' => 'mysql:host=localhost;dbname=schoolkit;charset=utf8mb4',
        'user' => 'your_username',
        'pass' => 'your_password'
    ],
    'storage_path' => __DIR__ . '/storage'
];

$schoolKit = SchoolKit::start($config);
$files = $schoolKit->files();
$files->setMaxFileSize(2 * 1024 * 1024); // 2MB