lundi 18 juin 2018

Downloading a file in Laravel

In Laravel, I have a page in which I am trying to provide download links to files and I am using Laravel Filemananager to upload these files.

The path to these files is set in the lfm.php config file as:

/*
|--------------------------------------------------------------------------
| Working Directory
|--------------------------------------------------------------------------
*/

// Which folder to store files in project, fill in 'public', 'resources', 'storage' and so on.
// You should create routes to serve images if it is not set to public.
'base_directory' => 'public',

'images_folder_name' => 'assets/uploads/images',
'files_folder_name'  => 'storage/files',

'shared_folder_name' => 'shares',
'thumb_folder_name'  => 'thumbs',

I followed the documentation on the Laravel website regarding the public and storage folders which says the following:

The public disk is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores these files in storage/app/public. To make them accessible from the web, you should create a symbolic link from public/storage to storage/app/public.

This convention will keep your publicly accessible files in one directory that can be easily shared across deployments when using zero down-time deployment systems like Envoyer.

To create the symbolic link, you may use the storage:link Artisan command:

php artisan storage:link

Before anything else I also set the following:

  1. In my .env I changed the app URL to: http://127.0.0.1:8000 which is where php artisan server spins up a local server.
  2. In filesystems.php I changed the public array to the following:

    'public' => [ 'driver' => 'local', 'root' => storage_path('app/public'), 'url' => env('APP_URL').'/storage', 'visibility' => 'public', ],

I then created the SymLink.

In my public directory I then had the below:

enter image description here

Which is a link to storage/app/public

Then I uploaded a file to shares, so in my storage folder I now had:

app/public/shares/name-of-file

To test this I have the following Controller and View:

Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class TemplateController extends Controller
{
    public function index()
    {
        $files = Storage::allFiles('files/shares');

        return view('pages.templates-and-tools.index', compact('files'));
    }
}

Relevant part in view

<table id="templates" class=" table-striped table-vaccancies table-responsive">
    <thead>
        <tr>
            <th class="col-md-1 col-xs-1">Type</th>
            <th class="col-md-6 col-xs-6">Name</th>
            <th class="col-md-2 col-xs-2">Category</th>
            <th class="col-md-2 col-xs-2">Modified</th>
            <th class="col-md-1 col-xs-1">Size</th>

        </tr>
        <tr class="warning no-result">
            <td colspan="5">
                <i class="fa fa-warning"></i>
                <h3 class="text-center"> No result</h3>
            </td>
        </tr>
    </thead>
    <tbody>


        @foreach ($files as $file) 
        @php 


        $date = Storage::lastModified($file); $date = gmdate("d/m/Y - H:i", $date); 
        $size = Storage::size($file); $size = ceil ($size/1000); 
        $url = Storage::url($file);

        $fileName = pathinfo($file)['filename']; 
        $Category = pathinfo($file)['dirname']; 
        $type = pathinfo($file)['extension'];
        $Category = basename(dirname( $file)) 
        @endphp

        <tr>
            <td></td>
            <td>
                <a href=" "></a>
            </td>
            <td></td>
            <td> </td>
            <td>kb</td>
        </tr>


        @endforeach

    </tbody>
</table>

The above grabs everything in the Storage folder under files/shares, I then just spit out a bunch of information about the file into each table cell.

The issue I'm facing is with this line:

<a href=""></a>

It returns the following error:

File not found at path: http:/127.0.0.1:8000/storage/files/shares/Hummingbird_Technologies_ES.pdf (View: C:\xampp\htdocs\my-newable\resources\views\pages\templates-and-tools\index.blade.php)

Which is a pretty straightforward error, but...

enter image description here

You can see that the file exists.

So, what am I doing wrong?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire