Framework: Confusing symlink behaviour

Created on 30 Jun 2017  路  2Comments  路  Source: laravel/framework

  • Laravel Version: 5.4.24
  • PHP Version: 7.0

When running php artisan storage:link (as per the documentation), a symlink between public/storage/ to storage/app/public is created. I realise this is by design, but it leads to some very confusing behaviour not covered by the documentation.

For example: If you storeAs a file to an avatars directory, it's stored in: storage/app/avatars and the path returned (for database storage) is: avatars/filename.jpg. This is outside the symlink. Instead we need to tell it storeAs to storage/app/public/avatars (which results in a path returned for database storage being: public/avatars/filename.jpg).

According to the documentation, all we need to do to refer to this file is asset('avatars/filename.jpg') in our code, but that doesn't work. (And neither does the string we stored: asset('public/avatars/filename.jpg').) Instead it needs to be asset('storage/avatars/filename.jpg').

Which means we need to do a str_replace('public', 'storage', asset($filename)) to make an application work as per the documentation's instructions :-/

I assume I'm doing something wrong, but this so very confusing!

Most helpful comment

It seems you're mixing up the internal storage path (avatars/filename.jpg), the real path of the file (storage/app/avatars/filename.jpg) and the public url to the file (storage/avatars/filename.jpg).

  1. I guess you're using the local disk when you should be using the public disk. It's the later one that the symlink is meant for.
  2. I also guess you're building your urls by hand instead of asking the storage for the url to the file. Storage::disk('public')->url('avatars/filename.jpg').

All 2 comments

It seems you're mixing up the internal storage path (avatars/filename.jpg), the real path of the file (storage/app/avatars/filename.jpg) and the public url to the file (storage/avatars/filename.jpg).

  1. I guess you're using the local disk when you should be using the public disk. It's the later one that the symlink is meant for.
  2. I also guess you're building your urls by hand instead of asking the storage for the url to the file. Storage::disk('public')->url('avatars/filename.jpg').

Ahhhhhhhhh! Thanks so much. That was a missing piece of the puzzle.

When I storeAs with a directory and filename, (eg. avatars/filename.jpg) the file IS saved in the correct location (storage/app/public/avatars/filename.jpg) and the DB has (avatars/filename.jpg). Great.

Unfortunately when I try to load the file via {{ asset($file) }} it predictably tries the location returned by the database. Am I supposed to just manually add the required storage/ to the file location? (e.g asset('storage/'.$file))

This seems odd. Again the documentation is unclear.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JamborJan picture JamborJan  路  3Comments

ghost picture ghost  路  3Comments

progmars picture progmars  路  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments

iivanov2 picture iivanov2  路  3Comments