I am doing a project in Laravel that consists of each user being able to save their own files and download them and I have found the problem that when I download the images they are not seen on my computer, instead in the Laravel folder where I store the files they work right. I am saving the files in storage/app/public/(user_id) and in turn it is linked to public/storage/(user_id) but it does not work, after downloading any type of image or video it appears on my computer as corrupt
That is my controller to download the files that i store in my project:
public function downloadFile(Request $request)
{
$ruta = $request->input('ruta');
$nombre = $request->input('filename');
$ruta = public_path('storage/' . $ruta);
return response()->download($ruta, $nombre);
}
That is my controller to upload the files that i store in my project:
public function store(Request $request)
{
$request->validate([
'archivo' => 'required|file|max:10240',
]);
$carpetaUsuario = Auth::id();
$archivo = $request->file('archivo');
$nombreArchivo = $archivo->getClientOriginalName();
$ruta = Storage::disk('public')->put($carpetaUsuario, $archivo);
$size_file = $archivo->getSize();
$size_file = $size_file / 1024;
$extension = pathinfo($ruta, PATHINFO_EXTENSION);
$archivo = new Archivo();
$archivo->nombre = $nombreArchivo;
$archivo->ruta = $ruta;
$archivo->usuario_id = Auth::user()->id;
$archivo->tipo = $extension;
$archivo->tamanio_archivo = $size_file;
$archivo->save();
return redirect()->back()->with('success', 'Archivo subido correctamente.');
}
That's my view where users see their own files and upload the files I store in my project, and can also delete them:
<!DOCTYPE html>
<head>
<link href="{{ asset('/css/app.css') }}" rel="stylesheet" type="text/css" >
<link href="{{ asset('/css/inicio.css') }}" rel="stylesheet" type="text/css" >
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<title>ArchiVault</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
<!-- <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> -->
</head>
<body>
<header>
<div class="logo">
<a href="index.html">
<img src="{{ asset ('images/logo.png')}}" alt ="logo de la tienda">
</a>
</div>
<nav>
@if (auth()->check())
@csrf
<a href="{{ route('logout') }}" class="nav-link">Cerrar sesión</a>
@else
<a href="{{ route('login') }}" class="nav-link">Iniciar sesión</a>
<a href="{{ route ('crear_usuario') }}" class="nav-link">Crear cuenta</a>
@endif
</nav>
</header>
<div class="subir-archivo">
<form action="{{ route('archivos.store') }}" method="POST" enctype="multipart/form-data" id="formulario-archivo">
@csrf
<input type="hidden" name="usuario_id" value="{{ Auth::id() }}">
<label for="archivo">Seleccionar archivo:</label>
<input class="form-control" type="file" name="archivo" id="archivo">
<button type="submit" id="subir_archivo">Subir archivo</button>
</form>
</div>
<!-- <form class="modal" action="{{ route('archivos.store') }}" method="POST" enctype="multipart/form-data" id="formulario-archivo">
@csrf -->
<!-- <span class="close">X</span> -->
<!-- <div class="content">
<span class="title">Sube tu archivo</span>
<p class="message">Select a file to upload from your computer or device.</p> -->
<!-- <input type="hidden" name="usuario_id" value="{{ Auth::id() }}">
<div class="actions">
<label for="archivo" class="button upload-btn">Elige un archivo</label>
<input type="file" id="archivo" name="archivo" style="display: none;">
</div>
<div class="result">
<div class="file-uploaded"><p></p></div>
</div>
</div>
</form> -->
<hr>
<h2>Mis archivos</h2>
<div class="archivos-list">
@foreach($archivos as $archivo)
<div class="archivo-card">
<p data-id="{{ $archivo->id }}">{{ $archivo->nombre }}</p>
<!-- <div class="descripcion">
<p>{{ $archivo->descripcion }}</p>
</div> -->
<form method="POST" action="{{ route('download.file', $archivo) }}">
@csrf
<input type="hidden" name="ruta" value="{{ $archivo->ruta }}">
<input type="hidden" name="filename" value="{{ $archivo->nombre }}">
<button type="submit" class="download-btn">
<span class="material-icons">file_download</span>
</button>
</form>
<form method="POST" action="{{ route('archivos.destroy', $archivo) }}">
@csrf
@method('DELETE')
<button type="submit" class="delete-btn"><span class="material-icons">delete</span></button>
</form>
</div>
@endforeach
</div>
<!-- <script src="{{ asset('/js/inicio.js') }}" type="text/javascript"></script> -->
@include('footer')
filesystems.php
'disks' => [
'local' => [
'driver' => 'local',
'root' => storage_path('app'),
],
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'visibility' => 'public',
]
'links' => [
public_path('storage') => storage_path('app/public'),
],