0

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'),
    ],
 

Pepe
  • 1
  • 1
  • 1
    "they are not seen on my computer" - what does that mean? How is that related to PHP? PHP cannot determine where to store file you download in your browser – Nico Haase Apr 21 '23 at 12:13
  • It means that if I download other types of files from that folder of my Laravel project from the view, it looks correctly, on the other hand, if I download an image from that folder, on my macbook in the preview program it tells me that the file is damaged – Pepe Apr 21 '23 at 12:24
  • Jave you checked it isn't? – Marcin Orlowski Apr 21 '23 at 12:35
  • The files must be accessible publicly to download. Did you run `php artisan storage:link`? – Mahdi Rashidi Apr 21 '23 at 12:39
  • 1
    What have you tried to resolve the problem? Where are you stuck? That's so little code that even using var_dump could help to see where this is going wrong – Nico Haase Apr 21 '23 at 12:40
  • I tried the php artisan storage:link but it doesnt work, I also tried to save the files in another folder that was not storage/app/uploads/(user_id) but it doesn't work either. the strange thing is that it only happens to me with multimedia files – Pepe Apr 21 '23 at 12:42
  • I also tried to use laravel media library to handle the download and upload of files but it didn't work either – Pepe Apr 21 '23 at 12:48
  • So ... `artisan storage:link` links a *certain* folder in your `storage` directory to your `public` directory. It's by default *not* the `app` folder in your storage directory. You can manipulate those folders in your `config/filesystem` config file – UnderDog Apr 22 '23 at 03:35
  • UnderDog, I am saving the files in sotrage/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 – Pepe Apr 22 '23 at 18:52

1 Answers1

-1

Hmmmm, i am not sure if i understand right, but your problem seems like problem with php configuration: Change the maximum upload file size

try this or changing rights to storage folder

chmod -R 777 public/storage
  • Please add some explanation to your answer such that others can learn from it. How would any of these two problems download files as corrupt? – Nico Haase Apr 24 '23 at 11:11