25

I want to retrieve public url for all the files stored using

storage::putFile('public/spares');

So, this is the issue I'm using

storage::files('public/spares');

but it provides this output from laravel storage directory

public/spares/image1.jpg
public/spares/image2.jpg
public/spares/image3.jpg

how can I get the public link for the above

http://localhost/laravel/public/storage/spares/image1.jpg
http://localhost/laravel/public/storage/spares/image2.jpg
http://localhost/laravel/public/storage/spares/image3.jpg

**Edit **

Sending last modified data of files to view

$docs = File::files('storage/document');
$lastmodified = [];
foreach ($docs as $key => $value) {
   $docs[$key] = asset($value);
   $lastmodified[$key] = File::lastmodified($value);
}
return view('stock.document',compact('docs','lastmodified'));

Is this correct

Daniel Euchar
  • 1,740
  • 5
  • 28
  • 45

5 Answers5

31

First you have to create the symbolic link from your public/storage directory to your storage/app/public directory so you can access the files. You can do that with:

php artisan storage:link

So then you can store your documents with:

Storage::putFile('spares', $file);

And access them as an asset with:

asset('storage/spares/filename.ext');

Check out the documentation on the public disk

Eric Tucker
  • 6,144
  • 1
  • 22
  • 36
  • what i wanted to achieve was get the list of files in the directory and display it in my view. The solution you have given works for single asset not for getting the full list. – Daniel Euchar May 10 '17 at 02:54
26

What about Storage::url? It works even for local storage.

You can find more here: https://laravel.com/docs/5.4/filesystem#file-urls

If you want to return all files' urls from the directory, you can do something like this:

return collect(Storage::files($directory))->map(function($file) {
    return Storage::url($file);
})

Don't forget to inject the \Illuminate\Filesystem\FilesystemManager instead of the Storage facade if you are looking for a not-facade way.

Edit:

There are 2 (or more) ways how you can deal with modified date:

Pass files to the view.

|ou can pass your Storage::files($directory) directly to the view and then in your template use following:

// controller:

return view('view', ['files' => Storage::files($directory)]);

// template:

@foreach($files as $file)
   {{ Storage::url($file) }} - {{ $file->lastModified }} // I'm not sure about lastModified property, but you get the point
@endforeach

Return an array:

return collect(Storage::files($directory))->map(function($file) {
     return [
         'file' => Storage::url($file),
         'modified' => $file->lastModified // or something like this
     ]
})->toArray()
Jakub Kratina
  • 644
  • 6
  • 14
5

I know this question is quite old, but i am putting this answer for anyone still having this issue from laravel 5.5+

To solve the issue update your filesystem's configuration by adding the 'url' key like below:

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

Hope it helps :)

Ewomazino Ukah
  • 2,286
  • 4
  • 24
  • 40
1

Well I figured out the solution, instead of searching the storage

$docs = Storage:files('public/spares');

we can search the symbolic link

$docs = File:files('storage/spares');

then run it through the asset() function to get the public URL. Is there a better solution?

louisfischer
  • 1,968
  • 2
  • 20
  • 38
Daniel Euchar
  • 1,740
  • 5
  • 28
  • 45
0
$file = Storage::disk('public')->put($folder, request()->file($key), 'public');
$file = Storage::url($file);
Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36
Hassan Khan
  • 15
  • 1
  • 1