Home > other >  Laravel server, upload to public_html rather than upload in project folder using laravel 8
Laravel server, upload to public_html rather than upload in project folder using laravel 8

Time:11-18

Hi I am using Blue shared Hosting and 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 separate the project folder and put all public file in public_html folder). but it returns to my project folder, not public_html one. please help me how can resolve that thanks.

Controller

if($request->has('image')){
            $imageName = Storage::disk('cms')->putFile('', $request->image);
            $home_slider->image  =   $imageName;
            $home_slider->save();
        }

filesystem.php

'cms' => [
            'driver' => 'local',
            'root' => public_path('uploads'),
            'url' => env('APP_URL').'uploads',
            'visibility' => 'public',
        ],

CodePudding user response:

Perhaps you can try changing:

$imageName = Storage::disk('cms')->putFile('', $request->image);

To:

$imageName = Storage::disk('local')->putFile('uploads', $request->image);

Assuming that the folder you want to upload your image to under public_html is named uploads

CodePudding user response:

I assume that you have one folder named uploads under public_html

If you don't want any custom image name . Just store file as it is. you can do like:-

          $imagename = $request->txtFile->store('uploads');  
          $imagename=str_replace('uploads/','',$imagename);
          $destinationPath = 'uploads';
          $file->move($destinationPath, $imagename);

If you want to give custom name, then you can do like this:-

for eg:

          $random = Str::random(10);
          $timestamp=strtotime(date('H:i:s')).$random;
          $customfilename=$slug.'-logo-'.$timestamp.'.'.$fileextension;
          $imagename = $request->txtFile->storeAs('uploads',$customfilename);  
          $imagename=str_replace('uploads/','',$imagename);
          $destinationPath = 'uploads';
          $file->move($destinationPath, $imagename);
  • Related