Home > Mobile >  Making new folder at public path in Laravel 9 not working
Making new folder at public path in Laravel 9 not working

Time:12-12

I'm working with Laravel 9 and I need to make a new folder like this:

public function make()
    {
        $ip = \Request::ip();

        $path = public_path().'/assets/captcha/'.$ip;
        File::makeDirectory($path, $mode = 0777, true, true);
        ...
    }

But when this method runs, it won't work and not creating a new folder based on the ip address of user.

However the $ip variable is already set properly.

So what's going wrong here?

I would really appreciate any idea or suggestion from you guys...

CodePudding user response:

By default you cannot make folders in you public_path (if you dont change file-system-permissions). You got the storage_path() helper that will give the correct folder where to store files.

However I'd recommend using the Storage facade instead. With it you can make folders on configured storage devices and skip filepaths in your code altogether (thats in config then).

$path = 'assets/captcha/'.$ip;    
if(!Storage::exists($path))
        Storage::makeDirectory($path);

If files should be available for download with public http access you can link the storage folder to the public folder in the filesystem.

  • Related