-5

I intend to insert image link like that "annonces\ August2020 \ image.jpg" into the db and image in storage but it gives me error Undefined variable: insert.

FileController.php

public function save(Request $request)
    {
       request()->validate(['file'  => 'required|mimes:doc,docx,pdf,txt,jpeg,jpg|max:2048',]);

       if ($files = $request->file('fileUpload')) {
           $destinationPath = 'public/file/'; // upload path
           $profilefile = date('YmdHis') . "." . $files->getClientOriginalExtension();
           $files->move($destinationPath, $profilefile);
           $insert['file'] = "$profilefile";
        }
        $check = Document::insertGetId($insert);
        return Redirect::to("file")
        ->withSuccess('Great! file has been successfully uploaded.');
    }

file.balde.php

<form action="/save" method="post" enctype="multipart/form-data">
                @csrf
                <div class="form-group">
                    <input type="file" class="form-control-file" name="file" id="file" aria-describedby="fileHelp">
                    <small id="fileHelp" class="form-text text-muted">Please upload a valid image file. Size of image should not be more than 2MB.</small>
                </div>
                <button type="submit" class="btn btn-primary">Submit</button>
            </form>

web.php

Route::get('file', 'fileController@index');
Route::post('save', 'fileController@save');
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
Aid Aid
  • 83
  • 1
  • 10

3 Answers3

0

first of all if you want to store the images then you should store it using Storage feature of laravel. let me show you only the code for uploading the image and save it in database.

if($request->hasFile('file')){
        $file = $request->file('file');
        $fileName = time().$file->getClientOriginalName();
        Storage::put('public/file'.$fileName,file_get_contents($file));
        //file is uploaded and now you can store the name of the file in database 
    }

please add use Storage on top of the controller file.

now you can access it in this way:

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

don't forget to run this command php artisan storage:link before uploading pictures in storage directory

Naveed Ali
  • 1,043
  • 7
  • 15
0
public function save(Request $request)
        {
           $insert=[];
           request()->validate(['file'  => 'required|mimes:doc,docx,pdf,txt,jpeg,jpg|max:2048',]);
    
           if ($files = $request->file('fileUpload')) {
               $destinationPath = 'public/file/'; // upload path
               $profilefile = date('YmdHis') . "." . $files->getClientOriginalExtension();
               $files->move($destinationPath, $profilefile);
               $insert['file'] = "$profilefile";
            }
            $check = Document::insertGetId($insert);
            return Redirect::to("file")
            ->withSuccess('Great! file has been successfully uploaded.');
        }

$insert is defined in this code, please try it.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
hammad khan
  • 57
  • 3
  • 6
0

Look at this line:

$insert['file'] = "$profilefile";

This says that in the presumably associated array called $insert the element with the key of 'file' will have the value of "$profilefile" from now on. Your code crashes, because $insert was not initialized. Apparently the interpreter of PHP was not as smart as you and did not know that $insert should be an associated array. You will need to have a line of the like of

$insert = [];

which would define insert as an array. Note that this line must be executed before you assign values to the elements of $insert.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175