Hasnainali9's avatar

Uploaded File issue

Hey just working around with a developer code to fix the issue which is unusual The form on the client-side is submitted successfully but on the server-side, the file was not received. Here is my Client Form.

<div>						


{ { Form::open(['route' => ['admin.settings.update', $settings->id], 'method' => 'POST', 'enctype' => 'multipart/form-data']) } }
    @csrf
    { { Form::hidden('_method', 'PUT') } }

    <div class="container-fluid mb-5">
        <div class="row">
            <div class="col-md-12">
                <h4 class="header-title my-3">Update Settings</h4>

                <div class="form-row">
                    <div class="col-md-6">
                        <div class="card-box">
                            <div class="form-group">
                                {{ Form::label('admin_logo', 'Admin Logo', ['class' => 'col-form-label']) }}
                                {{ Form::file('admin_logo', ['class' => 'dropify', 'data-max-file-size' => '2M', 'data-default-file' => asset($settings->admin_logo), 'accept' => '.jpg,.jpeg,.png']) }}
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="card-box">
                            <div class="form-group">
                                {{ Form::label('admin_favicon', 'Admin favicon', ['class' => 'col-form-label']) }}
                                {{ Form::file('admin_favicon', ['class' => 'dropify', 'data-max-file-size' => '2M', 'data-default-file' => asset($settings->admin_favicon), 'accept' => '.jpg,.jpeg,.png']) }}
                            </div>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="card-box">
                            <div class="form-group">
                                {{ Form::label('site_logo_light', 'Site Logo Light', ['class' => 'col-form-label']) }}
                                {{ Form::file('site_logo_light', ['class' => 'dropify', 'data-max-file-size' => '2M', 'data-default-file' => asset($settings->site_logo_light), 'accept' => '.jpg,.jpeg,.png']) }}
                            </div>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="card-box">
                            <div class="form-group">
                                {{ Form::label('site_logo_dark', 'Site Logo Dark', ['class' => 'col-form-label']) }}
                                {{ Form::file('site_logo_dark', ['class' => 'dropify', 'data-max-file-size' => '2M', 'data-default-file' => asset($settings->site_logo_dark), 'accept' => '.jpg,.jpeg,.png']) }}
                            </div>
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="card-box">
                            <div class="form-group">
                                {{ Form::label('site_favicon', 'Site Favicon', ['class' => 'col-form-label']) }}
                                {{ Form::file('site_favicon', ['class' => 'dropify', 'data-max-file-size' => '2M', 'data-default-file' => asset($settings->site_favicon), 'accept' => '.jpg,.jpeg,.png']) }}
                            </div>
                        </div>
                    </div>
                </div>

                <div class="card-box">
                    <div class="form-group">
                        {{ Form::label('site_banner_video', 'Site Banner Image', ['class' => 'col-form-label']) }}
                        {{ Form::file('site_banner_video', ['class' => 'dropify', 'data-max-file-size' => '20M', 'data-default-file' => asset($settings->site_banner_video), 'accept' => '.jpg,.jpeg,.png']) }}
                    </div>
                </div>

                <div class="card-box">
                    <div class="form-row">
                        <div class="form-group col-md-6">
                            {{ Form::label('site_title', 'Site Title', ['class' => 'col-form-label']) }}
                            {{ Form::text('site_title', $settings->site_title, ['class' => 'form-control', 'placeholder' => 'Site Title']) }}
                        </div>
                        <div class="form-group col-md-6">
                            {{ Form::label('seo_title', 'SEO Title', ['class' => 'col-form-label']) }}
                            {{ Form::text('seo_title', $settings->seo_title, ['class' => 'form-control', 'placeholder' => 'SEO Title']) }}
                        </div>
                    </div>
                    <div class="form-group">
                        {{ Form::label('seo_description', 'SEO Description', ['class' => 'col-form-label']) }}
                        {{ Form::text('seo_description', $settings->seo_description, ['class' => 'form-control', 'placeholder' => 'SEO Description']) }}
                    </div>
                </div>


                <div class="my-4">
                    <div class="custom-control custom-switch">
                        {{ Form::checkbox('preloader', true, $settings->preloader, ['class' => 'custom-control-input', 'id' => 'preloader']) }}
                        {{ Form::label('preloader', 'Pre Loader', ['class' => 'custom-control-label']) }}
                    </div>
                </div>
                {{ Form::submit('Update Settings', ['class' => 'btn btn-primary']) }}
            </div>
        </div>
    </div>
{{ Form::close() }}
</div>

Here is my Server Side Controller.

<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Setting;
use Illuminate\Http\Request;
use Image;

class SettingsController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('admin.auth:admin');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Setting  $setting
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $settings = Setting::first();
        if ($settings == null) {
            $setting = new Setting;
            $setting->save();
        }
        return view('admin.settings')->with('settings', Setting::first());
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Setting  $setting
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $settings = Setting::first();

        // Admin Logo
        if ($request->hasFile('admin_logo')) {
            $request->validate([
                'admin_logo' => 'required|image|mimes:jpg,jpeg,bmp,png,gif|max:2000',
            ]);
            $image = $request->file('admin_logo'); // Get File Name
            $imageExt = $image->getClientOriginalExtension();
            $directoryPath = '/uploads/settings';
            $destinationPath = public_path($directoryPath); // Set File Destination
            // Create Directory If Not Exist
            if (!file_exists($destinationPath)) {
                mkdir($destinationPath, 755, true);
            }
            $imageName = 'admin-logo-'.(date("Ymd").time()).'.'.$imageExt;
            $adminLogoNameToStore = $directoryPath.'/'.$imageName;
            // Resize And Save Image
            Image::make($image->getRealPath())->resize(640, 400, function ($constraint) {
                $constraint->aspectRatio();
            })->save($destinationPath.'/'.$imageName);

            if (\File::exists(public_path($settings->admin_logo))) {
                \File::delete(public_path($settings->admin_logo));
            }
            $settings->admin_logo = $adminLogoNameToStore;
        }


        // Admin Favicon
        if ($request->hasFile('admin_favicon')) {
            $request->validate([
                'admin_favicon' => 'required|image|mimes:jpg,jpeg,bmp,png,gif|max:2000',
            ]);
            $image = $request->file('admin_favicon'); // Get File Name
            $imageExt = $image->getClientOriginalExtension();
            $directoryPath = '/uploads/settings';
            $destinationPath = public_path($directoryPath); // Set File Destination
            // Create Directory If Not Exist
            if (!file_exists($destinationPath)) {
                mkdir($destinationPath, 755, true);
            }
            $imageName = 'admin-favicon-'.(date("Ymd").time()).'.'.$imageExt;
            $adminFaviconNameToStore = $directoryPath.'/'.$imageName;
            // Resize And Save Image
            Image::make($image->getRealPath())->resize(640, 400, function ($constraint) {
                $constraint->aspectRatio();
            })->save($destinationPath.'/'.$imageName);

            if (\File::exists(public_path($settings->admin_favicon))) {
                \File::delete(public_path($settings->admin_favicon));
            }
            $settings->admin_favicon = $adminFaviconNameToStore;
        }

        // Site Logo Light
        if ($request->hasFile('site_logo_light')) {
            $request->validate([
                'site_logo_light' => 'required|image|mimes:jpg,jpeg,bmp,png,gif|max:2000',
            ]);
            $image = $request->file('site_logo_light'); // Get File Name
            $imageExt = $image->getClientOriginalExtension();
            $directoryPath = '/uploads/settings';
            $destinationPath = public_path($directoryPath); // Set File Destination
            // Create Directory If Not Exist
            if (!file_exists($destinationPath)) {
                mkdir($destinationPath, 755, true);
            }
            $imageName = 'site-logo-light-'.(date("Ymd").time()).'.'.$imageExt;
            $siteLogoLightNameToStore = $directoryPath.'/'.$imageName;
            // Resize And Save Image
            Image::make($image->getRealPath())->resize(640, 400, function ($constraint) {
                $constraint->aspectRatio();
            })->save($destinationPath.'/'.$imageName);

            if (\File::exists(public_path($settings->site_logo_light))) {
                \File::delete(public_path($settings->site_logo_light));
            }
            $settings->site_logo_light = $siteLogoLightNameToStore;
        }

        // Site Logo Dark
        if ($request->hasFile('site_logo_dark')) {
            $request->validate([
                'site_logo_dark' => 'required|image|mimes:jpg,jpeg,bmp,png,gif|max:2000',
            ]);
            $image = $request->file('site_logo_dark'); // Get File Name
            $imageExt = $image->getClientOriginalExtension();
            $directoryPath = '/uploads/settings';
            $destinationPath = public_path($directoryPath); // Set File Destination
            // Create Directory If Not Exist
            if (!file_exists($destinationPath)) {
                mkdir($destinationPath, 755, true);
            }
            $imageName = 'site-logo-dark-'.(date("Ymd").time()).'.'.$imageExt;
            $siteLogoDarkNameToStore = $directoryPath.'/'.$imageName;
            // Resize And Save Image
            Image::make($image->getRealPath())->resize(640, 400, function ($constraint) {
                $constraint->aspectRatio();
            })->save($destinationPath.'/'.$imageName);

            if (\File::exists(public_path($settings->site_logo_dark))) {
                \File::delete(public_path($settings->site_logo_dark));
            }
            $settings->site_logo_dark = $siteLogoDarkNameToStore;
        }

        // Site Favicon
        if ($request->hasFile('site_favicon')) {
            $request->validate([
                'site_favicon' => 'required|image|mimes:jpg,jpeg,bmp,png,gif|max:2000',
            ]);
            $image = $request->file('site_favicon'); // Get File Name
            $imageExt = $image->getClientOriginalExtension();
            $directoryPath = '/uploads/settings';
            $destinationPath = public_path($directoryPath); // Set File Destination
            // Create Directory If Not Exist
            if (!file_exists($destinationPath)) {
                mkdir($destinationPath, 755, true);
            }
            $imageName = 'site-favicon-'.(date("Ymd").time()).'.'.$imageExt;
            $siteFaviconNameToStore = $directoryPath.'/'.$imageName;
            // Resize And Save Image
            Image::make($image->getRealPath())->resize(640, 400, function ($constraint) {
                $constraint->aspectRatio();
            })->save($destinationPath.'/'.$imageName);

            if (\File::exists(public_path($settings->site_favicon))) {
                \File::delete(public_path($settings->site_favicon));
            }
            $settings->site_favicon = $siteFaviconNameToStore;
        }

        // Banner Video
        if ($request->hasFile('site_banner_video')) {
            $request->validate([
                'site_banner_video' => 'required|file|mimes:mp4,mpg,mpv,mov,ogg,flv,avi|max:20000',
            ]);
            $video = $request->file('site_banner_video'); // Get File Name
            $videoExt = $video->getClientOriginalExtension();
            $directoryPath = '/uploads/settings';
            $destinationPath = public_path($directoryPath); // Set File Destination
            // Create Directory If Not Exist
            if (!file_exists($destinationPath)) {
                mkdir($destinationPath, 755, true);
            }

            $videoName = 'site-banner-video-'.(date("Ymd").time()).'.'.$videoExt;
            $videoNameToStore = $directoryPath.'/'.$videoName;

            $video->move($destinationPath, $videoName);

            if (\File::exists(public_path($settings->site_banner_video))) {
                \File::delete(public_path($settings->site_banner_video));
            }
            $settings->site_banner_video = $videoNameToStore;
        }

        $settings->preloader = ($request->input('preloader') == true) ? true : false;
        $settings->site_title = $request->input('site_title');
        $settings->seo_title = $request->input('seo_title');
        $settings->seo_description = $request->input('seo_description');

        if ($settings->save()) {
            return redirect()->route('admin.settings.edit', 1)->with('success', 'Settings Update Successful');
        } else {
            return redirect()->back()->with('error', 'Settings Update Failed');
        }

    }
}


Here is my Web.php File Code

Route::resource('settings', SettingsController::class)->only([
    'edit', 'update'
]);```
0 likes
9 replies
Snapey's avatar

please format your code by putting three backticks ``` before and after your code blocks

Snapey's avatar

You would get more support if you did not use the long neglected Laravel collective forms.

1 like
Hasnainali9's avatar

@Snapey could you pls look at the code & let me know the issue when I submit the form I receive an array of only 6 variable with input file text remain images field are empty even if I submit the file

Please or to participate in this conversation.