© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•3y ago•
3 replies
harps

Download private files

I'm trying to create a scenario where only logged in users can see uploaded files, I've seen it asked here a few times but I can't make sense of the answers.

I have this
    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Select::make('client_id')
                    ->relationship(name: 'client', titleAttribute: 'client_name'),
                Forms\Components\TextInput::make('project_name')
                    ->required()
                    ->maxLength(100),
                Forms\Components\FileUpload::make('shape_file')
                    ->preserveFilenames()
                    // directory is record id of the project
                    ->directory(fn ($record) => 'project-shapes/' . $record->id)
                    ->previewable(false)
                    ->downloadable()
                    ->disk('private')
            ]);
    }
    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\Select::make('client_id')
                    ->relationship(name: 'client', titleAttribute: 'client_name'),
                Forms\Components\TextInput::make('project_name')
                    ->required()
                    ->maxLength(100),
                Forms\Components\FileUpload::make('shape_file')
                    ->preserveFilenames()
                    // directory is record id of the project
                    ->directory(fn ($record) => 'project-shapes/' . $record->id)
                    ->previewable(false)
                    ->downloadable()
                    ->disk('private')
            ]);
    }


and this in the filesystem config

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
            'throw' => false,
        ],

        'private' => [
            'driver' => 'local',
            'root' => storage_path('app/private'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'private',
            'throw' => false,
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
            'throw' => false,
        ],

        'private' => [
            'driver' => 'local',
            'root' => storage_path('app/private'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'private',
            'throw' => false,
        ],

I have public sym linked.

Everything works including event listeners for deleting but I can't download the file from the form.

I would also like to have an icon link in the resource table to the file.

Thanks Dan
Solution
I resolved this by adding a route to my private file paths in web.php
use App\Http\Controllers\FileController;
Route::get('/private/project-shapes/{project_id}/{file}', [FileController::class, 'downloadFile'])->middleware(['auth'])->name('file.download');
use App\Http\Controllers\FileController;
Route::get('/private/project-shapes/{project_id}/{file}', [FileController::class, 'downloadFile'])->middleware(['auth'])->name('file.download');

And then my controller

class FileController extends BaseController
{
    public function downloadFile(String $project_id, $file)
    {
        if (Auth::id()) {
          return response()->download(storage_path('app/private/project-shapes/' . $project_id . '/' . $file));
        }
        else{
          abort(404);//not found
        }
        
    }
}
class FileController extends BaseController
{
    public function downloadFile(String $project_id, $file)
    {
        if (Auth::id()) {
          return response()->download(storage_path('app/private/project-shapes/' . $project_id . '/' . $file));
        }
        else{
          abort(404);//not found
        }
        
    }
}
Jump to solution
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

file download for private files
FilamentFFilament / ❓┊help
3y ago
FileUpload (Download Private)
FilamentFFilament / ❓┊help
3y ago
Spatie Medialibrary private files on s3
FilamentFFilament / ❓┊help
3y ago
FileUpload download/preview file from private storage
FilamentFFilament / ❓┊help
3y ago