简体   繁体   中英

How to Replace and Delete previous files uploaded, when Edit a record in Laravel 4.2

I use laravel 4.2, because this project has been around for years. :)

Now, how can i replace and delete previous files uploaded in public directory, when edit a record ? Given the following Controller code ( Update Methode ) :

public function update($id)
        {
            $idd = Sentry::getUser()->id;
            try {
                $validator = Validator::make(
                    Input::all(),
                    array(
                        'first_name' => 'required',
                        'last_name' => 'required',
                        'eng_certificate' => 'image|mimes:png,jpeg,jpg|max:1100',
                        'profile_img' => 'image|mimes:png,jpeg,jpg|max:1100',
                    )
                );
                if ($validator->passes()) {
                    $profile = User::find($id);
                    $profile->first_name = Input::get('first_name');
                    $profile->last_name = Input::get('last_name');    
                    if (Evidence::where('user_id', $idd)->count() == 0) {
                        $evidence = new Evidence;
                        $evidence->user_id = $idd;
                        $evidence->created_at = jDate::forge()->time();
                    } else {
                        $evidence = Evidence::where('user_id', $idd)->first();
                        $evidence->updated_at = jDate::forge()->time();
                    }
                    if (Input::hasFile('eng_certificate')) {
                        $image = Input::file('eng_certificate');
                        $destinationPath = 'uploads/evidence';
                        $filename = $image->getClientOriginalName();
                        $extension = $image->getClientOriginalExtension();
                        $image_filename = sha1($filename) . '-' . rand(0, 1000) . '.' . $extension;
                        $image_uploadSuccess = $image->move($destinationPath, $image_filename);
                        if ($image_uploadSuccess) {
                            $evidence->eng_certificate = $image_filename;
                            $evidence->save();
                        }
                    }
                    if (Input::hasFile('profile_img')) {
                        $image = Input::file('profile_img');
                        $destinationPath = 'uploads/evidence/profile_img/';
                        $filename = $image->getClientOriginalName();
                        $extension = $image->getClientOriginalExtension();
                        $image_filename = sha1($filename) . '-' . rand(0, 1000) . '.' . $extension;
                        $image_uploadSuccess = $image->move($destinationPath, $image_filename);
                        if ($image_uploadSuccess) {
                            $evidence->profile_img = $image_filename;
                            $evidence->save();
                        }
                    }
                    $profile->save();
                    return Redirect::route('cpanel.home')->with('success', 'اطلاعات پروفایل شما با موفقیت بروز گردید.');
                }
                else {
                    return Redirect::back()->withInput()->withErrors($validator->messages());
                }
            }
            catch (LoginRequiredException $e) {
                return Redirect::back()->withInput()->with('danger', $e->getMessage());
            }
        }

How can I fix it? Thank you.

  1. find out current file location
  2. use php unlink function https://www.w3schools.com/php/func_filesystem_unlink.asp

You can interact with the filesystem by using: Laravel Filesystem

For example:

Storage::delete('file.jpg');

See: https://laravel.com/docs/5.8/filesystem#deleting-files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM