Home > Software engineering >  user uploaded images should be stored in the project like public/userprofileimage.jpg or it will be
user uploaded images should be stored in the project like public/userprofileimage.jpg or it will be

Time:02-04

Consider the case where a user uploaded his profile image and it was placed in the project as public/userprofileimage.jpg when the repository was at version 1.0.0.

The project was then pulled from GitHub, changed, or new code was added, and it was pushed to the repository, becoming version 2.0.0.

How can I add the latest uploaded photos to the repository each time a user uploads something?

Will the user-uploaded picture be in 2.0.0?

or the image will be lost 

What should I do with the user-uploaded images if that's the case so they don't get lost in version control?

  if($request->hasFile('image1') && $request->hasFile('image2') && $request->hasFile('image3') && $request->hasFile('image4')){
        $formFields['media'] = 
        $request->file('image1')->store('postUploads','public') . ',' 
        . $request->file('image2')->store('postUploads','public') . ',' 
        . $request->file('image3')->store('postUploads','public') . ',' 
        . $request->file('image4')->store('postUploads','public');
    }

and did that

php artisan storage:link

l retrieve the image like that

<img src="{{asset('storage/' . $image)}}"  alt="">

is that the right way?

thanks

CodePudding user response:

A better approach

use Illuminate\Support\Facades\Validator;

public function store(Request $request){

 $validate = Validator::make($request->all(), [
   $request->file('image1') => 'required|mimes:jpg,png,jpeg',
   $request->file('image2') => 'required|mimes:jpg,png,jpeg',
   $request->file('image3') => 'required|mimes:jpg,png,jpeg',
   $request->file('image4') => 'required|mimes:jpg,png,jpeg',
 ]);

if( $validate->fails() ){
 return response($validate->errors(), 400);
}

//anything below here means validation passed. You can then store your images

$path1 = $request->file('image1')->store('profile_pictures','public');
$path2 = $request->file('image2')->store('profile_pictures','public');
$path3 = $request->file('image3')->store('profile_pictures','public');
$path4 = $request->file('image4')->store('profile_pictures','public');

}

Note that this can even be further simplified by saving your images to an array. I did not use that approach as I am not sure whether all the images are profile images or will be used differently.

Your images will be stored in /storage/profile_pictures and Laravel will automatically generate an image name for you.

On your view you can call the images using the asset helper as below

<img src="{{ asset($path1) }}"/>

This is assuming you are sending the image paths individually, which also can be simplified based on your application. Hope this give you an idea.

  • Related