Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
349 views
in Technique[技术] by (71.8m points)

forms - Can't store image upload on public folder laravel

I'm trying to create an register form that includes an image upload. When I submit the form, it stores an "/private/var/tmp/" path for the image on database and the image is not stored anywhere in the public folder. I don't know what I might be doing wrong. Please help.

My config/filesystems:

'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
        ],

My controller CreateNewUser:

class CreateNewUser implements CreatesNewUsers
{
    use PasswordValidationRules;

    protected function validator(array $input)
    {
        return Validator::make( $input, [
            'firstname' => ['required', 'string', 'max:255'],
            'lastname' => ['required', 'string', 'max:255'],
            'email' => [
                'required',
                'string',
                'email',
                'max:255',
                Rule::unique(User::class),
            ],
            'mobile' => ['required', 'int'],
            'country' => ['required', 'string', 'max:255'],
            'password' => $this->passwordRules(),
            'db_upload' => ['required', 'image'] //This the image I want to upload
        ]);
    }

    public function register(Request $request)
    {
        $this->validator($request->all())->validate();

        $imageUrl = $this->storeImage($request);

        $input = $request->all();
        $input['db_upload'] = $imageUrl;

        $user = $this->create($input);
        
        $this->guard()->login($user);

        return $this->registered($request, $user)
                        ?: redirect($this->redirectPath());
    }

    protected function storeImage(Request $request) {
        $path = $request->file('db_upload')->store('public/uploads');

        return substr($path, strlen('public/'));
    }


    public function create(array $input)
    {

        $user = User::create([
            'firstname' => $input['firstname'],
            'lastname' => $input['lastname'],
            'email' => $input['email'],
            'mobile' => $input['mobile'],
            'country' => $input['email'],
            'password' => Hash::make($input['password']),
            'db_upload' => $input['db_upload'],
            
        ]);

            $user->notify(new WelcomeEmailNotification());

              return $user;       
    }
}
question from:https://stackoverflow.com/questions/65862100/cant-store-image-upload-on-public-folder-laravel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Change the line of code that saves the file from:

$path = $request->file('db_upload')->store('public/uploads');

to

$path = $request->file('db_upload')->store('uploads', 'public');

The first argument is the path and the second argument is the disk you want to save to.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...