Laravel file upload issue in public folder
creativeitem-project
PROOP

7 months ago

Hi,

I have uploaded a Laravel application. But the application uploads its files in public directory instead of the storage directory.

So in railway hosted application when I try to upload any images or files its not uploading in the public directory.

I am new with railway. Maybe I missed something I am not sure. Need some guidance.

Note: Folder is present in public directory where file should upload and its working on local server or in any other shared hustings.

TIA.

$10 Bounty

2 Replies

7 months ago

What’s happening

Railway’s container filesystem is ephemeral and small. Don’t write directly to public/. Use Laravel’s storage/app/public (via the public disk) and a symlink, backed by a Railway Volume.

---

Do this (4 steps)

1. Attach a Volume to your Laravel service

Mount it at /app/storage (so storage/... becomes persistent). Railway also sets RAILWAY_VOLUME_MOUNT_PATH.

---

2. Use the public disk (not raw public/ paths);.env:

env

FILESYSTEM_DISK=public

App code:

php

Storage::disk('public')->put('uploads/my.jpg', $content);

This writes to storage/app/public.

---

3. Create/refresh the symlink

Ensure each deploy runs:

bash

php artisan storage:link || true

(Optionally also: php artisan config:cache && php artisan route:cache.)

Make sure permissions are OK:

bash

chmod -R ug+rw storage bootstrap/cache

---

4. Redeploy to apply changes.

Sanity checks

bash

echo "VOL=$RAILWAY_VOLUME_MOUNT_PATH"
ls -al public/storage                 # should be a symlink
php artisan tinker --execute="Storage::disk('public')->put('probe.txt','ok')"
ls storage/app/public/probe.txt       # should exist (and persist across deploys)

---

Alternative

For heavy uploads or CDN needs, switch the disk to S3/Cloudinary instead of local disk.


daheltechdev
HOBBY

7 months ago

On Railway, writing directly to the public folder won’t work the same way it does on shared hosting or local environments — that folder is read-only in production. Instead, Laravel recommends storing uploaded files in the storage/app/public folder and then using php artisan storage:link to create a symbolic link from public/storage to that directory.


Loading...