upload_file()
protected function upload_file(array $config): ?array
Description
Uploads a file using Trongate's inbuilt File class.
How It Works:
- It instantiates a
Fileobject. - It calls the
upload()method of theFileclass, passing the provided$configarray as an argument. - The
upload()method handles the file upload process, including validation, security checks, and moving the file to the target destination. - If the upload is successful, it returns an array containing details about the uploaded file. If the upload fails, it throws an exception.
Parameters
| Parameter | Type | Description | Default |
|---|---|---|---|
| $config | array | An associative array containing upload configuration options. These options are passed directly to the File class's upload() method. For details, refer to the File class documentation. |
N/A |
Return Value
| Type | Description |
|---|---|
| array|null | Returns an associative array containing details about the uploaded file if the operation is successful. The array includes:
|
Example Usage
Best Practices
- Validate Configuration: Ensure the upload configuration is valid and the destination path is accessible before calling this method.
- Use Random Filenames: When handling sensitive files, use
'make_rand_name' => trueto generate random filenames and avoid conflicts. - Handle Exceptions: Use try-catch blocks to handle exceptions gracefully, especially when working with user-provided paths.