Created
January 30, 2025 06:55
-
-
Save arif98741/35a4dd9c9f598b0b74ce30e4652ea0ba to your computer and use it in GitHub Desktop.
get file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Illuminate\Support\Facades\Auth; | |
use Illuminate\Support\Facades\Storage; | |
use Illuminate\Support\Facades\Response; | |
function getFile($fileName) | |
{ | |
// Check if the user is logged in | |
if (!Auth::check()) { | |
return response('Unauthorized', 401); // Return an unauthorized response | |
} | |
// Construct the file path | |
$filePath = storage_path('app/uploads/' . $fileName); | |
// Check if file exists | |
if (!file_exists($filePath)) { | |
return response('File not found', 404); // Return file not found response | |
} | |
// Return the file as a response | |
return Response::download($filePath); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Route::get('/storage/uploads/{file}', 'FileController@getFile'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment