Reputation: 366
I deployed a simple Laravel (v5.7) app on Heroku with this config:
filesystems.php
:
'public' => [
'driver' => 'local',
'root' => storage_path('app/public'), /* app/storage/app/public */
'url' => env('PUBLIC_STORAGE_URL'), /* http://myApp.herokuapp.com/public/storage */
'visibility' => 'public',
],
Heroku tree:
app
Procfile
app
database
storage
app
public
public
storage /* symlink to /app/storage/app/public */
The application works fine on Heroku except I cannot upload files to the public storage folder while it works fine locally.
I am using the following code:
request()->file($userSelectedFile)->store($userFolderName);
An example generated path looks like:
http://myApp.herokuapp.com/public/storage/userFolderName/kwooAGW0CrChzDbUUhOdSXsoLDI99rd1enPd22ZU.png
but no file is created in app/storage/app/public/
.
How can I handle user uploads properly on Heroku?
Upvotes: 3
Views: 2239
Reputation: 136977
Heroku provides an ephemeral filesystem. It can be used for temporary storage, but anything saved there will be lost when your dynos restart. This happens frequently (at least once per day).
If you need long-term storage, Heroku recommends using a third-party service like Amazon S3. Laravel supports this approach natively:
Laravel provides a powerful filesystem abstraction thanks to the wonderful Flysystem PHP package by Frank de Jonge. The Laravel Flysystem integration provides simple to use drivers for working with local filesystems, Amazon S3, and Rackspace Cloud Storage. Even better, it's amazingly simple to switch between these storage options as the API remains the same for each system.
Assuming you want to use S3, add league/flysystem-aws-s3-v3
and league/flysystem-cached-adapter
to your dependencies, then update your config/filesystems.php
accordingly. There should be a sample S3 configuration in there already.
Upvotes: 1