Skip to content

File Uploads & Downloads ​

How modules store uploaded files safely outside the web root with LimsFileStorage, serve them via FileResponse, and link to them cleanly from Angular views.

Architecture ​

Never write uploads directly into web-accessible folders or serve raw filesystem paths. Inject LIMS\Platform\FileStorage\LimsFileStorage into a dedicated file/attachment service in your module's Core/Services/ (e.g. OrderAttachmentService).

LimsFileStorage:

  • Stores files under .env FILE_STORAGE_ROOT with generated filenames (preventing path traversal and execution).
  • Tracks every version, supports soft deletes & restores, validates extensions against detected MIME types, and logs access in system_file_log.
  • Exposes only a random 32-character reference to callers — internal IDs and disk paths never leave PHP.

Permissions are your service's job

LimsFileStorage does not check permissions. Always verify authorization in your module's service before calling upload, uploadMany, get, replace, or delete. Always pass your module key as the second argument to get($reference, 'orders') so references from other modules return null.

Storing uploads ​

Submit forms with <input type="file" name="attachments[]" multiple> via postForm() (which sends FormData, so files arrive in $request->files('attachments')):

php
<?php

declare(strict_types=1);

namespace LIMS\Modules\Orders\Core\Services;

use Labmin\FileStorage\Schemas\SystemFile;
use LIMS\Modules\Orders\Core\Exceptions\OrderNotFoundException;
use LIMS\Modules\Orders\Core\Exceptions\OrderWorkflowException;
use LIMS\Platform\FileStorage\LimsFileStorage;

final class OrderAttachmentService
{
    private const MODULE = 'orders';
    private const RECORD_TYPE = 'order';
    private const RULES = ['maxSize' => '20MB'];

    public function __construct(private readonly LimsFileStorage $files) {}

    /** @param array<string, mixed> $uploads */
    public function add(int $orderId, array $uploads, ?string $category = null): void
    {
        $refused = [];

        foreach ($this->files->uploadMany($uploads, self::MODULE, self::RECORD_TYPE, $orderId, self::RULES, $category) as $result) {
            if ($result['error'] !== null) {
                $refused[] = "{$result['name']}: {$result['error']}";
            }
        }

        if ($refused !== []) {
            // Throwing rolls back any DB writes made earlier in the same request.
            throw new OrderWorkflowException('Some files were rejected. ' . implode(' ', $refused));
        }
    }

    /** @return array{SystemFile, string} */
    public function download(string $reference): array
    {
        $file = $this->files->get($reference, self::MODULE);
        $path = $file === null ? null : $this->files->absolutePath($file);

        if ($file === null || $path === null || $file->recordType !== self::RECORD_TYPE) {
            throw new OrderNotFoundException('attachment');
        }

        $this->files->record('downloaded', $file);

        return [$file, $path];
    }
}

Serving downloads (FileResponse) ​

Return FileResponse from a #[Get] controller method:

php
#[Get]
public function download(array $query): FileResponse
{
    [$file, $path] = $this->attachments->download((string) ($query['ref'] ?? ''));
    $inline = ($query['inline'] ?? '') === '1';

    return new FileResponse(
        $path,
        $file->originalName,
        download: !$inline,
        contentType: $file->mimeType,
    );
}

Linking from Angular (raw=1 & download) ​

Never forget raw=1 and download

  1. Plain <a href> links cannot send an X-App-Request header. Without raw=1 in the query string, admin/engine.php wraps the binary file inside the LIMS HTML layout (producing a broken .html download).
  2. The LIMS shell displays a full-screen "Loading" overlay on page unload. Adding the download attribute (or target="_blank" for inline browser previews) prevents the overlay from staying up.
ts
// Build download & inline preview URLs in your <module>Links.ts
export function attachmentUrl(ref: string, inline = false): string {
  return url("download", {
    feature: "orders",
    query: { ref, inline: inline ? "1" : null, raw: "1" },
  });
}
html
<!-- Download to disk -->
<a fusionButton class="btn-secondary font-normal" [href]="attachmentUrl(file.reference)" download>
  Download
</a>

<!-- Preview inline in a new browser tab -->
<a fusionButton class="btn-secondary font-normal" [href]="attachmentUrl(file.reference, true)" target="_blank">
  View
</a>

Labmin LIMS Module Developer Documentation