# Files


The request object of Wolff has an array of uploaded files, each one of the elements in that array is an instance of Wolff\Core\Http\File.

The request object and the file objects have some methods that can simplify the process of uploading files to the server.

Setting Options

fileOptions(array $arr)

With the fileOptions method you can specify the options for uploading files, passing an associative array as parameter.

The array can have the following keys:

  • dir: The directory where the files will be uploaded (relative to the project root folder).

  • extensions: A comma separated string with the allowed file extensions.

  • max_size: The maximum file size allowed (in KB).

  • override: true for overriding files with the same path in the upload process, false for not overriding them.

    'dir'        => 'public',
    'extensions' => 'jpg, png, bmp',
    'max_size'   => 1024,
    'override'   => true,

File methods


get(string $key): mixed

Returns the value of the specified key.


That would be equivalent to $_FILES['profile_image']['size'].


upload([string $name]): bool

Uploads the file and stores it in the server with the given name.

This method returns true if the file has been successfully uploaded, false otherwise.

If no name is provided, the original file name will be used instead.



Let's create a simple file upload example.

First create the form.


<form action="{{ url('upload') }}" method="post" enctype="multipart/form-data">
    <input type="file" name="image">
    <button type="submit">Upload!</button>

Then create both routes, one that will display the form and another that will take care of the file upload.


// Form view
Route::get('file', function ($req) {

// Upload
Route::post('upload', function ($req) {
        'dir'        => 'files',
        'extensions' => 'jpg,png',
        'max_size'   => 2048,
        'override'   => false

    if ($req->file('image')->upload()) {
        echo 'File has been successfully uploaded';
    } else {
        echo 'An error has occurred';

So, if the client uploads a file that complies with:

  • A jpg or png extension.
  • No bigger than 2048KB.
  • Its path is not taken already in the server

The message File has been successfully uploaded will appear, otherwise the message An error has occurred will appear.

Documentation made with
Something is wrong in here? Make a issue/pull request on the repo.