Zong
Zong

Reputation: 71

Laravel How to upload EXCEL file to database?

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?

Upvotes: 0

Views: 15646

Answers (4)

shubiao-yao
shubiao-yao

Reputation: 36

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/

Upvotes: 0

dekts
dekts

Reputation: 830

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.');      
} 

Upvotes: 3

Chirag Patel
Chirag Patel

Reputation: 111

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

and you are done.

Upvotes: 0

Pavel
Pavel

Reputation: 936

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.

Upvotes: 1

Related Questions