Skip to content

Instantly share code, notes, and snippets.

@arif98741
Created January 30, 2025 06:55
Show Gist options
  • Save arif98741/35a4dd9c9f598b0b74ce30e4652ea0ba to your computer and use it in GitHub Desktop.
Save arif98741/35a4dd9c9f598b0b74ce30e4652ea0ba to your computer and use it in GitHub Desktop.
get file
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);
}
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