Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Developer654079525's avatar

Custom disk in Laravel

I wanted to be able to generate files directly in the public folder, so I modified the filesystems.php to include a custom disk location:

        'my_disk' => [
            'driver' => 'local',
            'root' => storage_path('../public'),
            'visibility' => 'private',
        ],

Is this inherently unsafe, and are there alternatives to write to the ../public folder, one level up?

0 likes
8 replies
ramonrietdijk's avatar

I'd not recommend storing the files in your public folder directly. Files that are uploaded or generated should reside in the storage directory. That being said, they must be validated if you make them directly accessible. Even if you use the public disk that Laravel offers by default. Depending on how your webserver is configured, files may be executed.

Using a symbolic link like described on the Laravel documentation would be the easiest. Do you require the files to be accessible from the base URL directly?

cosmic_learning's avatar

Yeah, pointing a disk to the ../public folder like that can be risky. The public directory is meant for files accessible via the web, so if you write files there without careful control, you might accidentally expose sensitive data or let users upload harmful files.

martinbean's avatar

@developer654079525 Why on earth are you trying to write files to your public directory? This is a huge security risk.

If a user uploads a malicious file, they’ll then have a way to immediately execute that file, compromising both your application and the server it’s hosted on.

Glukinho's avatar
Level 31

Having a custom filesystem is not unsafe by itself, it may become unsafe if you allow external users, for example, upload anything there without sanitation.

As long as you generate blog-rss.xml and sitemap.xml by yourself (by scheduled artisan command for example) I don't see how your approach is unsafe.

1 like
Snapey's avatar

I agree with what has been said above by @glukinho but I wouldn't create a disk for just this.

Just use public_path() when saving the file.

1 like

Please or to participate in this conversation.