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

Viewed 1280

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!

2 Answers

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';
});

The public disk is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores these files in storage/app/public.

When using the local driver, all file operations are relative to the root directory defined in your filesystems configuration file. By default, this value is set to the storage/app directory. Therefore, the following method would store a file in storage/app/file.txt:

Storage::disk('local')->put('file.txt', 'Contents');

Or in your case in storage/app/images/file.txt

$file->storeAs(public_path('images/'), $filename);

You can change where these files are stored by changing the filesystems configuration file.

'local' => [
    'driver' => 'local',
    'root' => public_path(),
],
Related