1

how can i create a folder using a controller function?

(Routes and all are good, but don't know how to make the folder)

I have a simple form for example:

<form method="POST" action="{{ route('admin.projects.store') }}" enctype="multipart/form-data">
      <div class="form-group">
        <label name="title">Slug:</label>
        <input type="text" id="slug" name="slug" placeholder="ejemplo-de-slug" class="form-control form-control-sm">
      </div>
</form>

Route in web.php:

Route::post('projects/postUpload', ['uses' => 'AdminController@storeProject', 'as' => 'admin.projects.store']);

I want to put to the folder the same name of the slug.

public function storeProject()
    {
        return ;
    }

Know how to do it?

1
  • use mkdir(path,mode,recursive,context) function Commented Jul 24, 2017 at 10:00

1 Answer 1

1

The best practice is to use Storage:

Storage::makeDirectory($directory);

It will create a new folder on specified disk (local storage, AWS etc).

If you want to create a new folder on the local disk, you can use File facade:

File::makeDirectory('/path/to/directory', 0775);
Sign up to request clarification or add additional context in comments.

4 Comments

Now i will try it. If doesn't work i will edit this comment, if works i edit the question.
I have to put: File::makeDirectory('/assets/img/projects/hola', 0775,true); and i get the error: mkdir() permission denied. i'm using nginx
@LluísPuigFerrer it's not related to Laravel. You can use chmod command to fix permissions, but do not do that, it's a terrible idea. Use storage directory to store uploaded stuff. Or use services like AWS. But do not use assets directory. If you want to have access to uploaded files, you need to create a symbilic link. Read this: laravel.com/docs/5.4/filesystem#the-public-disk
the problem is that i need to create a folder when i create a new project. And then, in the form will be 2 pics and i need store it in the folder.. a bit confusing for me, i will try to make a directory changing permision and then use storage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.