1

I store files in storage.

No I tried to generate link that open file like:

http://[temp.com]/storage/tests/de139607636857fade861a3c2c472643.txt

But it does not work.

How to open file from storage by URL address?

POV
  • 11,293
  • 34
  • 107
  • 201

3 Answers3

8

Files in the storage are not accessible by url by default

You could use Public Disk. For that you need to create symbolic link from public/storage to storage/app/public

From Larvel 5.3 and up, you have artisan command that will help you to create symlink

php artisan storage:link

If you are using older version of Laravel, you can find an answer how to create symbolic link here


Another approach would be to create new disk "uploads", by editing the file config/filesystems.php

'disks' => [
    'local' => [
        'driver' => 'local',
        'root'   => storage_path(),
    ],
    'uploads' => [
        'driver' => 'local',
        'root'   => public_path() . '/uploads',
    ],
]

To store files in this location

Storage::disk('uploads')->put($file_name, $file_content);

And to get the file

asset('uploads/'. $file_name)
ljubadr
  • 2,174
  • 1
  • 20
  • 24
2

The storage directory exists outside the web root, as some of the stuff in there shouldn't necessarily be publicly accessible.

https://laravel.com/docs/5.5/filesystem#the-public-disk

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.

ceejayoz
  • 176,543
  • 40
  • 303
  • 368
  • Then how to be? Can I make link to storage directory using htaccess? – POV Oct 17 '17 at 20:37
  • This is full path to files: `/public_html/storage/app/tests` – POV Oct 17 '17 at 20:38
  • That is not the standard structure for a Laravel app. Does `public_html/storage/app/tests/de139607636857fade861a3c2c472643.txt` exist? Can the webserver's user access it? Why are you not using Laravel's built-in locations and functionality for this? – ceejayoz Oct 17 '17 at 20:47
0

I strongly recommend not to modify the Laravel's directory structure.

What I would do is to create a URL to download the file, for example:

Route::get('download/{file}', 'DownloadsController@index');

And in that method's controller, put some logic to assert that the file exists and serve it with a response download, for example

public function index($file)
{
    $filePath = storage_path('tests/'.$file);
    if (! file_exists($filePath)) {
        // Some response with error message...
    }

    return response()->download($filePath);
}

Then you can download your file with a link like this

http://[temp.com]/download/de139607636857fade861a3c2c472643.txt

The controller's way allows you to make some authentication checks before serving the file for example, or to count how many times the file was downloaded.

Lloople
  • 1,827
  • 1
  • 17
  • 31
  • 1
    While this does allow for extra functionality, I prefer @ljubadr's answer. Proxying files through Laravel requires more resources than a static file download handled by the webserver. – Jeffrey Oct 18 '17 at 09:11