david001's avatar

Uploading file gives error

I need to upload .xlsx file to database . I have 3 column in db value,pin_num,password_num my code is like this

 public function import(Request $request)
    {
        if($request->hasFile('file'))
        {
            $files = $request->file('file');

            $file = fopen($files, "r");
            while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
            {
                 $excel = new Excel;
                 $excel->value = $emapData[0];
                 $excel->pin_num = $emapData[1];
                 $excel->password_num = $emapData[2];
               
                 $excel->save();
                
            }
            
        }
       return Redirect::to('dashboard/file/view');

This upload .csvfile successfullly but it gives error on uploading.xlsx file Error is like this

Undefined offset: 1
in AdminController.php line 36
at HandleExceptions->handleError('8', 'Undefined offset: 1', '/var/www/html/project/app/Http/Controllers/Backend/Admin/AdminController.php', '36', array('request' => object(Request), 'files' => object(UploadedFile), 'file' => resource, 'emapData' => array('PK!q9+p��[Content_Types].xml ��(�̔MN�0��H�!�%n��j�?K��ؓƪc[���g��'), 'excel' => object(Excel))) in AdminController.php line 36

How to upload xlsx file?How to modify this code?

0 likes
5 replies
ejdelmonico's avatar

Try a package for that. A quick Google search will bring up a few. Reading a csv is easy but xslx is not. excel_reader2.php

david001's avatar

Now iam using Laravel-Excel

my code is like this

$results = Excel::load('public/palsvoiceaccount.xlsx')->get();
//how to get dynamic file in place of  "public/palsvoiceaccount.xlsx"
 //dd($results);
  foreach($results as $rows)
  {
    foreach($rows as $row)
    {
      
    Excell::create([
        
        'value'=>$row->value,
        'pin_num'=>$row->pin_no,
        'password_num'=>$row->password_no
    ]);

     var_dump('product insered');
  }
}

my question is how to putfile namein load function. i need to browse file from desktop.how to get dynamic file in place of public/palsvoiceaccount.xlsx

Please or to participate in this conversation.