Skip to content

Existing Files ​

Edit forms may pass existing files to the form so they appear in the UI.

From a Storage Disk ​

The ExistingFile::fromDisk() method returns one ExistingFile for a string path and an array for an array of paths. Image previews use a temporary URL that expires after five minutes by default. To change that behavior, pass the optional expiration, withPreview, and metadata arguments:

php
use InertiaUI\Forms\FileUploads\ExistingFile;

$existingFile = ExistingFile::fromDisk('public', 'avatars/photo.jpg');
$withoutPreview = ExistingFile::fromDisk('public', 'documents/report.pdf', withPreview: false);

The ExistingFile::fromFilesystem() method accepts an already resolved Laravel filesystem instance with the same path, preview, and metadata options.

From Media Library ​

The ExistingFile::fromMediaLibrary() method turns Media Library items into existing files:

php
// $product is a model with a "photos" media collection...
$existingFiles = ExistingFile::fromMediaLibrary($product->getMedia('photos'));

$thumbnails = ExistingFile::fromMediaLibrary(
    $product->getMedia('photos'),
    previewConversion: 'thumbnail',
);

The method accepts one media model or an iterable, and uses a five-minute preview expiration unless you pass another value. To keep the media items selectable without generating preview URLs, use fromMediaLibraryWithoutPreview() instead.

Image previews use a temporary URL when the disk supports one, then fall back to the Media Library model's normal URL. To preview a generated conversion, pass previewConversion. A conversion still waiting on its queue falls back to the original media until it's ready.

Existing files serialize to a browser value with an encrypted key and display fields:

json
{
    "key": "encrypted-existing-file-token",
    "id": "stable-file-identifier",
    "identifier": "stable-file-identifier",
    "filename": "photo.jpg",
    "name": "photo",
    "previewUrl": "https://example.com/photo.jpg",
    "preview_url": "https://example.com/photo.jpg",
    "mimeType": "image/jpeg",
    "mime_type": "image/jpeg",
    "size": 12345,
    "size_in_bytes": 12345
}

Selected existing files submit their key. Removed files leave that key out of the submitted value.

Token Scoping ​

Temporary upload keys and existing file keys are scoped, and they expire. Stateful requests bind keys to the session. Stateless requests bind them to the authenticated guard, provider, model, and user.

With session authentication, form routes and upload routes may use different guards as long as they resolve the same user. To revoke a session's upload keys immediately, invalidate the session on logout.

Managed upload endpoints and existing file serialization require either a session or an authenticated user. Without that scope, upload endpoints return 422 before writing any bytes. Place upload routes, and forms with existing files, behind session or authentication middleware.

Existing file keys expire after file_uploads.existing_files.lifetime seconds, which defaults to one hour. You may adjust it as described in Existing Files Configuration.