ActiveManage Docs ← Back to activemanage.co.uk

Securing Private Files with fetchfile.php

fetchfile.php is the authenticated file-serving endpoint that gates access to every file stored in a private path. It checks who's asking, what they want, whether they're allowed to have it, and then streams the bytes (or refuses).

Sequence diagram: Browser → /fetchfile.php?id=abc → check session → check permission on linked record → fetch bytes from S3 → stream to browser with appropriate Content-Type and Content-Disposition

How It Works

  1. Request hits /fetchfile.php?id=<file-id>&token=<short-lived>.
  2. Server validates the token (signature + expiry).
  3. Server looks up the file record. If linked to a parent record, server checks the user can see the parent.
  4. Server fetches bytes (from S3, local disk, or wherever the file store points).
  5. Server streams the response with the right Content-Type and a Content-Disposition that triggers display or download as appropriate.
  6. Access logged.

Security Properties

  • No direct URL access: Even if someone learns the underlying storage path, they can't fetch it directly.
  • Permission inheritance: If you can't see the order, you can't see its attached invoice.
  • Token expiry: URLs included in emails expire after 24 hours; share-with-others URLs can be set shorter.
  • Audit trail: Every fetch is logged with user, file and IP.

Performance Tuning

  • Enable byte-range support so video players can seek.
  • Use signed S3 URLs for large files (handoff the streaming to S3 directly while still doing auth on the way in).
  • Cache permission lookups for hot files within a request to avoid repeat queries.

Worked Examples

  • Patient record access: Doctor opens patient record; PDF reports linked. Clicking a PDF goes through fetchfile.php — confirms the doctor has access to the patient, then streams.
  • Order invoice link: Order confirmation email contains a fetchfile URL with a 7-day token. After 7 days the customer signs in normally to see invoices.
  • Embedded video: Internal training portal uses fetchfile-served videos; player byte-range support lets users skip to chapters efficiently.