creating a folder once user registred laravel 5

1.3k views Asked by At

I would like to create a folder inside Storage folder in Laravel 5, once you register and pick your username, a folder with that user will be created for you.

If you created user : john5500 a folder inside Storage will be created with 'john5500' and will belong only to that user.

3

There are 3 answers

8
Jirennor On BEST ANSWER

Mark, see the code below.

This code I use to create a new user in my database.

Information about ManagementCreateRequest $Request can be found via this URL.

Laravel Controller Validation

In short I'm validating my input via Controller Validation in Laravel. After the validation passes I get all the data from the validation in the variable $Request.

After that I create the user as below. After creating the user I send a redirect to the management page. This page contains an overview of all the users in the database.

public function store(ManagementCreateRequest $Request)
{
    // Create user
    Management::create($Request->all());

    // Return view
    return redirect('management')
        ->with('Success', 'User created.');
}

If I would to create a directory I would do it like this.

public function store(ManagementCreateRequest $Request)
{
    // Create user
    Management::create($Request->all());

    // Create directory
    File::MakeDirectory('/path/to/directory' . $Request->username);

    // Return view
    return redirect('management')
        ->with('Success', 'User created.');
}

Replace /path/to/directory with the actual path to your storage directory. For example: Under CentOS my storage directory would be.

/var/www/Site Name/storage

Don't forget to replace 'Site Name' with the name of your Laravel site.

More detailed information about File:makeDirectory can be found via this link:

Laravel Creating Directory

2
Emeka Mbah On

You can use Laravel File Facade:

File::makeDirectory($path, $mode = 0755, $recursive = false, $force = false);

Ensure storage is writable

public function create(array $data)
{


            //return
            $user = User::create([
                    'name' => $data['name'],
                    'username'=>$data['username'],
                    'email' => $data['email'],
                    'password' => bcrypt($data['password']),

            ]);


            \File::makeDirectory(storage_path($data['username']));

            return $user;


}
2
fahad.hasan On

Lravel 5 comes with an excellent filesystem. You could simply do:

Storage::makeDirectory($directory);

See the documentation for more details: http://laravel.com/docs/5.0/filesystem#basic-usage