Search code examples
laravelserverupload

Laravel server, upload to public_html rather than upload in project folder


I've been trying surfing around this site looking for answers, but nothing works for me, i want to upload an image that save in public_html folder, instead of saving it in project folder (i seperate the project folder and put all public file in public_html folder). Already bind the public.path but it returns to my project folder, not public_html one.

My image upload controller

public static function uploadSubmit($request, $type, $for)
{
    if($request->hasFile('photos'))
    {
        $allowedfileExtension=['jpg','png'];
        $files = $request->file('photos');
        foreach($files as $file)
        {
            $extension = $file->getClientOriginalExtension();

            $check=in_array($extension,$allowedfileExtension);
            //dd($check);
            if($check)
            {       
                    $idx = Image::create([
                    'type' => $type,
                    'ids' => $for,
                    'ext' => $extension,
                    ])->id;
                    $filename = $idx . '.' . $file->getClientOriginalExtension();
                    $filename = $file->storeAs(public_path('images/'), $filename);                
            }
        }
    }
}

i already do the public path bind like this in public_html/index.php

$app->bind('path.public', function() {
return __DIR__;
});

Thank you!


Solution

  • First, add config to filesystem.php on config folder:

    'disks' => [
    
            ...
    
            'public_html' => [
                'driver' => 'local',
                'root' => public_path(),
                'visibility' => 'public',
            ],
    
            ...
    
        ],
    

    Second, store image with code:

    $file->storeAs('images', $filename, 'public_html')
    

    P/S: Remember to configure the correct path for the public_html directory:

    $app->bind('path.public', function(){
        return __DIR__.'/../../public_html';
    });