Upload image to two different folder locations using Laravel 5.8

前端 未结 2 558
野性不改
野性不改 2021-01-28 05:44

This Code save image in only one folder. I want to upload the image at the same time in two different folders, example folder-one and folder-two

my controler

相关标签:
2条回答
  • 2021-01-28 06:04

    In controller:

    public function store(Request $request){
    
            if($request->User_jpeg != ''){ //check file has selected 
    
                $file = $request->User_jpeg;
    
                $path = base_path('public/folder-one/');
    
                $filename = time() . '_' . $file->getClientOriginalName();
    
                $file->move($path, $filename); 
    
                \File::copy($path.$filename,base_path('public/folder-two/'.$filename));
            }
    
            user::create([
                'photo_jpeg' => $filename,
            ]);
    
        }
    

    In route file (web.php):

    Route::post('save-image', 'YourController@store');
    
    0 讨论(0)
  • 2021-01-28 06:08

    Please make sure these things. if you are going to update file on different location.

    1. The folder must have the writtable permission.
    2. Directory path should be defined absolute and point to correct location.

    Now change verify the changes in code as follow.

    $fileName = time() . '.' .$request->file('User_jpeg')->getClientOriginalExtension();
    
    $storageLocation = '../../WEBSITE-FILE/TEAM/USER';  //it should be absolute path of storage location.
    
    $request->file('User_jpeg')
            ->storeAs($storageLocation, $fileName);
    
    $request->file('User_jpeg')
            ->storeAs($storageLocation . '/User_Profile_Image', $fileName);
    

    Edits:

    As per the requested current status, try this.

    public function store(Request $request) { 
        $this->validate($request, [ 'image' => 'required|image|mimes:jpeg,png,jpg|max:2048', ]); $input['image'] = time().'.'.$request->image->getClientOriginalExtension();
    
        $request->image->move(public_path('folder-a'), $input['image']);
    
        $fileSrc = public_path('folder-a') . $input['image'];
        $fileDest = public_path('folder-b') . $input['image'];
    
        \File::copy($fileSrc, $fileDest);
    
        Service::create($input); 
    
        return back()->with('success',' CREATED SUCCESSFULLY .'); 
    }
    
    0 讨论(0)
提交回复
热议问题