0

My Laravel version is 5.6.and PHP is 7. I found some upload sample on the internet, but most of them do not work. Have anyone can provide a complete example to me? I found a similar case on the Internet. Laravel 5.6 Excel and CSV import export using maatwebsite example

Unfortunately, it doesn't work, too.

Here is my source code. blade.php

<form class="form-upload" method="POST" action="{{url('/excelUpload')}}" enctype="multipart/form-data">
            {{ csrf_field() }}
             <div class="form-group">
                <label for="file">excel upload</label>
                <input type="file" id="file" name="ex_file">
                <p class="help-block">!!!</p>
             </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default">cancel</button>
                <button type="submit" class="btn btn-primary" id="upload_f">submit</button>

controller.php

public function excelUpload(Request $request){

         if($request->hasFile('StudentUploadSample')){
             return "yes i have a file";
         }
         return "nofile";
    }

However, the result always show "nofile". I didn't use any "use". Should I use something? The example on the web page does not use any "use", too. In addition, I read some of the websites that the files will be placed under the storage folder, but I can't find my file "StudentUploadSample.xlsx" in the storage folder.

Have anyone can provide a complete example to me or how to solve this problem?

2
  • 1
    it will return "nofile" because your input name is different with your request.. your input name is ex_file, but you request StudentUploadSample. It will return 'nofile' Commented Apr 10, 2018 at 6:45
  • Voting for close because it has a typo error. You should use your file name as StudentUploadSample or call hasFile on ex_file Commented Apr 10, 2018 at 6:53

4 Answers 4

3

Replace the name of file and use the reference site where you will get the proper code with explanation.

Reference site like: http://www.expertphp.in/article/laravel-5-maatwebsite-import-excel-into-db-and-export-data-into-csv-and-excel

public function importFileIntoDB(Request $request){
    if($request->hasFile('sample_file')){
        $path = $request->file('sample_file')->getRealPath();
        $data = \Excel::load($path)->get();
        if($data->count()){
            foreach ($data as $key => $value) {
                $arr[] = ['name' => $value->name, 'details' => $value->details];
            }
            if(!empty($arr)){
                \DB::table('products')->insert($arr);
                dd('Insert Record successfully.');
            }
        }
    }
    dd('Request data does not have any files to import.');      
} 

Sign up to request clarification or add additional context in comments.

3 Comments

yes it work , and thank you. but... new issue ...... the error message show this "Call to undefined method Maatwebsite\Excel\Excel::load()"
Thank you very much
1

You use the wrong name of the file in your controller:

if($request->hasFile('ex_file')){
     return "yes i have a file";
  }

So fix that, and try to check.

1 Comment

yes it work , and thank you. but... new issue ...... the error message show this "Call to undefined method Maatwebsite\Excel\Excel::load()" do you kbow
0

replace <input type="file" id="file" name="ex_file"> with <input type="file" id="file" name="StudentUploadSample">

and you are done.

1 Comment

yes it work , and thank you. but... new issue ...... the error message show this "Call to undefined method Maatwebsite\Excel\Excel::load()" do you kbow
0

you can use laravel excel.

address:https://github.com/Maatwebsite/Laravel-Excel

/** * my Controller code * */

$updateFile = $request->file('update_file');

$fileSize = (int) filesize($updateFile);

$fileExtension = $updateFile->getClientOriginalExtension();

$filePath = $updateFile->getRealPath();

$excelData = Excel::load($filePath)->get()->toArray();

or you can use: https://phpspreadsheet.readthedocs.io/en/develop/

1 Comment

ah... i am using it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.