I want to import some data from excel using phpSPreadsheet, but my upload file showed doesn’t exist, so try to look for where is the tmp_name, I use getStream but it doesn’t work, so I move it first into my webroot and load the file, it is success but not a good way
I need your help how I can do
this is my code
$this->loadModel('Students');
$group_id = $this->request->getData()['group_id'];
$files = $this->request->getUploadedFiles();
$files['import']->getStream();
$files['import']->getSize();
$files['import']->getClientFileName();
$myname = $this->request->getData()['import']->getClientFileName();
$myext = substr(strchr($myname, "."), 1);
$mypath = "upload/imports/".$myname;
$files['import']->moveTo(WWW_ROOT.$mypath);
if ($myext == 'xls') {
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
}else if($myext == 'xlsx'){
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
}
$spreadsheet = $reader->load(WWW_ROOT.$mypath);
$sheet = $spreadsheet->getActiveSheet()->toArray();
foreach ($sheet as $key => $excel){
if ($key == 0) {
continue;
}
$data = [
'nis' => $excel[1],
'name' => $excel[2],
'group_id' => $group_id,
];
// debug($data);
// die();
$student = $this->Students->newEmptyEntity();
$student = $this->Students->patchEntity($student, $data);
if ($excel[1] != null AND $excel[2] != null) {
if ($this->Students->save($student)) {
$this->Flash->success(__('The student has been saved.'));
}
}else{
$this->Flash->error(__('The student could not be saved. Please, try again.'));
}
}
return $this->redirect(['action' => 'index']);
}