public function parseFile(Request $r) { try { $file = $r->file('file'); $config = \Config::get('user_import'); if (!in_array($file->getClientMimeType(), $config['valide_mime_type'])) { throw new \Exception("File is not valid", 1); } $statusCode = 200; $parse = UserImport::parseFileImport($r); $response = ['items' => $parse['items'], 'file' => $parse['file'], 'tableHeader' => $config['table_header'], 'mapping' => $config['default_maping']]; } catch (\Exception $e) { $response = ["error" => 'File invalid']; $statusCode = 400; } finally { return \Response::json($response, $statusCode); } }
public static function parseFileImport(\Illuminate\Http\Request $r) { $result = array('items' => '', 'file' => ''); $config = \Config::get('user_import'); $generateName = uniqid() . '.' . $r->file('file')->getClientOriginalExtension(); $result['file'] = pathinfo($generateName)['basename']; $r->file('file')->move($config['path_price'], $result['file']); //$delimiter $excel = Excel::load($config['path_price'] . $result['file'], function ($reader) { $reader->noHeading(); }); if ($excel->getTotalRowsOfFile() > 10) { $excel = $excel->take(3)->get()->toArray(); } else { $excel = $excel->get()->toArray(); } $result['items'] = UserImport::formaterDataFile($excel, $generateName); return $result; }