Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a public directory restricted in laravel?

I am trying to restrict non loggedin users from downloading contents, what I have uploaded in a public directory.I am currently working on Laravel 5.2. So still now what I have done, I just a make a URL which checks the file name and folder name and download the files. In that way, I am hiding my parent folder directory from users. But this is not a good practice because if anybody finds the file URL then they can access it without logging in.

This is my download script:

public function getFile(Request $request)
{
          *************
          *************
 $folderFilePath = public_path('my file directory');
 return response()->download($folderFilePath, $filename, []);
}

So, is there a way that can restrict downloads from a particular directory for non logged in users in laravel? Thanks a lot in advance.

like image 809
Ishaque Javed Avatar asked Jan 30 '18 10:01

Ishaque Javed


People also ask

What is bootstrap folder in Laravel?

The bootstrap directory contains the app.php file which bootstraps the framework. This directory also houses a cache directory which contains framework generated files for performance optimization such as the route and services cache files. You should not typically need to modify any files within this directory.

What is public folder in laravel?

The files and folders in laravels public folder are meant to be web accessible. For security, all other files and folders in the laravel framework should not be web accessible. Moving the index. php to laravels root will break the framework and defy best practices.

Where is storage path Laravel?

Laravel's filesystem configuration file is located at config/filesystems.php . Within this file, you may configure all of your filesystem "disks". Each disk represents a particular storage driver and storage location.


1 Answers

You should keep the files in the storage directory which is not accessible for users. Then these files will be protected and authenticated users will still be able to download them:

if (auth()->check()) { 
    $folderFilePath = storage_path('my file directory');
    return response()->download($folderFilePath, $filename, []);
}
like image 137
Alexey Mezenin Avatar answered Nov 07 '22 18:11

Alexey Mezenin