Files
The Files
class provides a simple and secure way to handle file uploads and manage stored files.
Constructor
Creates a new Files instance with the specified storage path and configuration.
Parameters
Name | Type | Required | Description |
---|---|---|---|
$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
Stores an uploaded file.
Parameters
Name | Type | Required | Description |
---|---|---|---|
$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/...', ...]
}
Deletes a file from storage.
Parameters
Name | Type | Required | Description |
---|---|---|---|
$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');
Checks if a file exists in storage.
Parameters
Name | Type | Required | Description |
---|---|---|---|
$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');
Gets the full absolute path to a file in storage.
Parameters
Name | Type | Required | Description |
---|---|---|---|
$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'
Gets information about a file in storage.
Parameters
Name | Type | Required | Description |
---|---|---|---|
$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');
Creates a new directory within the storage path.
Parameters
Name | Type | Required | Description |
---|---|---|---|
$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');
Sets the allowed MIME types for file uploads.
Parameters
Name | Type | Required | Description |
---|---|---|---|
$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']);
Sets the maximum allowed file size for uploads.
Parameters
Name | Type | Required | Description |
---|---|---|---|
$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