SagorIslam's avatar

File upload not properly and update also but images working perfectly

Here is my STORE code

public function store(Request $request) {
        $request->validate([
            'bgImage' => 'required|image|max:2000|mimes:jpeg,bmp,png',
            'aboutMeImage' => 'required|image|max:2000|mimes:jpeg,bmp,png',
            'aboutMeHeading' => 'required|string',
            'aboutMeDignity' => 'required|string',
            'aboutMeDescription' => 'required|string',

            'aboutMeCV' => 'required|file|max:2000'
        ]);

        $aboutStore = new AboutUs();

    //Image uploading for about us background image
        //Find form bgImage and storing into a variable
        $bgImage = $request->file('bgImage');
        //Creating image upload time
        $currentDate = Carbon::now()->toDateString();

        //Using if statement and ensuring form data is available
        if (isset($bgImage)){
            //Creating image slug
            $slug = str_slug("about-us-bg-image");
            //Make unique name for image
            $bgImageName = $slug. '-'. $currentDate. '-'. uniqid(). '.'. $bgImage->getClientOriginalName();

            //Checking image storage folder, if not available then create a folder
            if (!Storage::disk('public')->exists('aboutUs')){
                Storage::disk('public')->makeDirectory('aboutUs');
            }

            //Save image and resize image
            $aboutUsBgImage = Image::make($bgImage)->resize(1920, 450)->stream();
            //And now put the image into storage disk
            Storage::disk('public')->put('aboutUs/'.$bgImageName,$aboutUsBgImage);

            //And save data into the database
            $aboutStore->bgImage = $bgImageName;
        }

        //Find form aboutMeImage and storing into a variable
        $aboutMeImage = $request->file('aboutMeImage');

        //Using if statement and ensuring form data is available
        if (isset($aboutMeImage)){
            //Creating image slug
            $slug = str_slug($request->aboutMeHeading);
            //Make unique name for image
            $aboutMeImageName = $slug. '-'. $currentDate. '-'. uniqid(). '.'. $aboutMeImage->getClientOriginalName();

            //Checking image storage folder, if not available then create a folder
            if (!Storage::disk('public')->exists('aboutUs')){
                Storage::disk('public')->makeDirectory('aboutUs');
            }

            //Save image and resize image
            $aboutUsImage = Image::make($aboutMeImage)->resize(600, 450)->stream();
            //And now put the image into storage disk
            Storage::disk('public')->put('aboutUs/'.$aboutMeImageName,$aboutUsImage);

            //And save data into the database
            $aboutStore->aboutMeImage = $aboutMeImageName;
        }

        $aboutStore->aboutMeHeading = $request->aboutMeHeading;
        $aboutStore->aboutMeDignity = $request->aboutMeDignity;
        $aboutStore->aboutMeDescription = $request->aboutMeDescription;

        //Find form aboutMeCV and storing into a variable
        $aboutMeCV = $request->file('aboutMeCV');

        //Using if statement and ensuring form data is available
        if (isset($aboutMeCV)){
            //Creating cv slug
            $slug = str_slug("about-me-cv");
            //Make unique name for cv
            $aboutMeCVName = $slug. '-'. $currentDate. '-'. uniqid(). '-'. $aboutMeCV->getClientOriginalName();

            //Checking cv storage folder, if not available then create a folder
            if (!Storage::disk('public')->exists('aboutUs')){
                Storage::disk('public')->makeDirectory('aboutUs');
            }

            //And now put the cv into storage disk
            Storage::disk('public')->put('aboutUs/'.$aboutMeCVName,$aboutMeCV);

            //And save data into the database
            $aboutStore->aboutMeCV = $aboutMeCVName;
        }

        $aboutStore->save();

        return redirect()->route('about.index')->with('success', 'About content saved successfully !');
    }

Here is my UPDATE code

$aboutUpdate = AboutUs::findOrFail($id);

    //Image uploading for about us background image
        //Find form bgImage and storing into a variable
        $bgImage = $request->file('bgImage');
        //Creating image upload time
        $currentDate = Carbon::now()->toDateString();

        //Using if statement and ensuring form data is available
        if (isset($bgImage)){
            //Creating image slug
            $slug = str_slug("about-us-bg-image-updated");
            //Make unique name for image
            $bgImageName = $slug. '-'. $currentDate. '-'. uniqid(). '.'. $bgImage->getClientOriginalName();

            //Checking image storage folder, if not available then create a folder
            if (!Storage::disk('public')->exists('aboutUs')){
                Storage::disk('public')->makeDirectory('aboutUs');
            }

            //Delete old image from database
            if (Storage::disk('public')->exists('aboutUs/'. $aboutUpdate->bgImage)){
                Storage::disk('public')->delete('aboutUs/'. $aboutUpdate->bgImage);
            }

            //Steam image and resize image
            $aboutUsBgImage = Image::make($bgImage)->resize(1920, 450)->stream();
            //And now put the image into storage disk
            Storage::disk('public')->put('aboutUs/'.$bgImageName,$aboutUsBgImage);

            //And save data into the database
            $aboutUpdate->bgImage = $bgImageName;
        }

        //Find form aboutMeImage and storing into a variable
        $aboutMeImage = $request->file('aboutMeImage');

        //Using if statement and ensuring form data is available
        if (isset($aboutMeImage)){
            //Creating image slug
            $slug = str_slug($request->aboutMeHeading. '-'.'updated');
            //Make unique name for image
            $aboutMeImageName = $slug. '-'. $currentDate. '-'. uniqid(). '.'. $aboutMeImage->getClientOriginalName();

            //Checking image storage folder, if not available then create a folder
            if (!Storage::disk('public')->exists('aboutUs')){
                Storage::disk('public')->makeDirectory('aboutUs');
            }

            //Delete old image from database
            if (Storage::disk('public')->exists('aboutUs/'. $aboutUpdate->aboutMeImage)){
                Storage::disk('public')->delete('aboutUs/'. $aboutUpdate->aboutMeImage);
            }

            //Save image and resize image
            $aboutUsImage = Image::make($aboutMeImage)->resize(500, 450)->stream();
            //And now put the image into storage disk
            Storage::disk('public')->put('aboutUs/'.$aboutMeImageName,$aboutUsImage);

            //And save data into the database
            $aboutUpdate->aboutMeImage = $aboutMeImageName;
        }

        $aboutUpdate->aboutMeHeading = $request->aboutMeHeading;
        $aboutUpdate->aboutMeDignity = $request->aboutMeDignity;
        $aboutUpdate->aboutMeDescription = $request->aboutMeDescription;

        //Find form aboutMeCV and storing into a variable
        $aboutMeCV = $request->file('aboutMeCV');

        //Using if statement and ensuring form data is available
        if (isset($aboutMeCV)){
            //Creating cv slug
            $slug = str_slug("about-me-cv");
            //Make unique name for cv
            $aboutMeCVName = $slug. '-'. $currentDate. '-'. uniqid(). '-'. $aboutMeCV->getClientOriginalName();

            //Checking cv storage folder, if not available then create a folder
            if (!Storage::disk('public')->exists('aboutUs')){
                Storage::disk('public')->makeDirectory('aboutUs');
            }

            //Delete old cv/document from database
            if (Storage::disk('public')->exists('aboutUs/'. $aboutUpdate->aboutMeCV)){
                Storage::disk('public')->delete('aboutUs/'. $aboutUpdate->aboutMeCV);
            }

            //And now put the cv into storage disk
            Storage::disk('public')->put('aboutUs/'.$aboutMeCVName,$aboutMeCV);

            //And save data into the database
            $aboutUpdate->aboutMeCV = $aboutMeCVName;
        }

        $aboutUpdate->save();

        return redirect()->route('about.index')->with('success', 'About content updated successfully !');
    }

Here I attached images via dropbox.

https://www.dropbox.com/s/b3mannrzyrzful2/Screen%20Shot%202020-03-04%20at%202.24.50%20PM.png?dl=0

https://www.dropbox.com/s/03n860jdtp007vj/Screen%20Shot%202020-03-04%20at%202.25.19%20PM.png?dl=0

NOTE: Some days ago I posted a similar post with was directly public directory and today I post Storage directory related.

Thanks

0 likes
4 replies
SagorIslam's avatar

all files upload perfectly but cv/document file creates a folder and then save cv/document file with a new name. Update section the same as the store section. please check the dropbox images.

But I want to save as like image file.

Snapey's avatar

you want us to wade through all this code, and then go and check a different question , and then look at screenshots?

Its not going to happen

Try reducing your code to the simplest test case

Please or to participate in this conversation.