Trongate Way Docs

Processing Uploads

The submit() method is the heart of the file manager. It validates the input, uploads the physical file, stores the metadata in the database, and redirects to the manage page. This follows the same pattern introduced in the Basic CRUD chapter: validate, process, redirect.

The Complete submit() Method

PHP
public function submit(): void {
    $this->gatekeeper();

    $this->validation->set_rules(
        'doc_name', 'Document Name',
        'required|min_length[2]|max_length[255]'
    );

    $this->validation->set_rules(
        'userfile', 'File',
        'allowed_types[pdf,txt,csv,doc,docx,'
        . 'xls,xlsx,zip]'
        . '|max_size[10240]'
    );

    $result = $this->validation->run();

    if ($result === true) {
        $doc_name = strip_tags(post('doc_name'));

        $config['destination']
            = 'modules/file_manager/files';
        $config['upload_to_module'] = true;
        $config['make_rand_name'] = true;

        $file_info = $this->file->upload($config);

        $data['doc_name'] = $doc_name;
        $data['file_name'] = $file_info['file_name'];
        $data['file_path'] = $file_info['file_path'];
        $data['file_size'] = $file_info['file_size'];
        $data['file_type'] = $file_info['file_type'];

        $this->model->insert_file($data);

        set_flashdata(
            'The file was successfully uploaded.'
        );
        redirect('file_manager/manage');
    } else {
        $data['view_module'] = 'file_manager';
        $data['view_file'] = 'create';
        $this->templates->admin($data);
    }
}

The Upload Config Array

The $config array passed to $this->file->upload() controls where and how the file is stored:

Key Value Effect
destination modules/file_manager/files Store files in a dedicated directory within the module
upload_to_module true Tell the File module the destination is relative to the module path
make_rand_name true Generate a random 10-character filename (preserving extension)

Using upload_to_module = true keeps uploaded files inside the module directory, keeping them organized and separate from your public-facing assets. The random filename prevents users from guessing file URLs and guards against filename collisions.

The upload() Return Array

When the upload succeeds, $this->file->upload() returns an array with four properties:

Key Example Value Description
file_name report_k3m9n1.pdf The random filename on disk
file_path ../modules/file_manager/files/report_k3m9n1.pdf The relative filesystem path
file_size 89201 Size in bytes
file_type application/pdf MIME type detected by the File module

All four properties are stored in the database exactly as returned. The file_path is particularly important - it is the value you will use with other File module methods like delete() and download().

What Happens When Validation Fails

If validation fails (e.g., the file type is not allowed, or the file exceeds the size limit), the method re-renders the upload form with validation error messages. Crucially, the file never reaches the filesystem - the Validation module checks the temporary uploaded file for type, size, and malicious content before the File module ever moves it to its destination.

Creating the Upload Directory

Before the first upload, you need to create the destination directory and ensure it is writable by the web server:

BASH
mkdir -p modules/file_manager/files
chmod 755 modules/file_manager/files

The 755 permission allows the owner full access, and everyone else read and execute access. If the directory does not exist, $this->file->upload() will throw an exception.

We're continually improving the Trongate documentation. If anything is incorrect, unclear, incomplete, or could be better, we'd genuinely appreciate your input.

Share your thoughts in the Documentation Feedback.

Leave Feedback About This Page