Home / Snippets / How to Create Directory in Laravel Storage if Not Exists
How to Create Directory in Laravel Storage if Not Exists cover

How to Create Directory in Laravel Storage if Not Exists

4.1K

3 years ago

0 comments

To programmatically create a directory in Laravel storage you can make use of the "Storage" facade provided by Laravel. The method is "makeDirectory()" and this accepts the path of the directory.
<?php

Storage::makeDirectory();
Do note that it supports a nested path so you can go as deep as you want it to.
<?php

Storage::makeDirectory('foo/bar/baz');

How to specify the Storage Disk?


To specify the storage disk for example "s3" or the "public" disk you can call the "disk()" method as follows.
<?php

Storage::disk('public')->makeDirectory('some/awesome/directory');
Do note that you will have to execute the code for Laravel to create the directory. For example creating a route endpoint to create the directory.
<?php

Route:get('create-directory', function () {
    Storage::disk('public')->makeDirectory('some/awesome/directory');
});
Now when you visit the "/create-directory" endpoint the directory will be created.
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

If you like our tutorial, support us by being our Patreon or buy us some coffee ☕️

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this