By kartagis on
I'm working on a module to import a Excel file and using PhpSpreadsheet class. Now, if my uploaded file is out of public://, I have no problems at all importing it. But if I have it in public://, I get PHP message: Uncaught PHP Exception InvalidArgumentException: "File "" does not exist." at File.php line 137. Line 137 throws an exception if the file is not a file, but dpm says it is. What am I doing wrong? Below is my code:
public function submitForm(array &$form, FormStateInterface $form_state) {
$connection = \Drupal::database();
$fid = $form_state->getValue("xlsx_upload");
$input = File::load(reset($fid));
$input->setPermanent();
$uri = \Drupal::service('file_system')->realpath($input->getFileUri());
//$uri = '/home/joe/file.xslx'; <--- this works perfectly.
$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($uri);
$spreadsheet = $reader->load($filename);
$dataArray = $spreadsheet->getActiveSheet()->toArray();
$connection->truncate("mitacan")->execute();
$connection->query("SET NAMES utf8");
foreach ($dataArray as $dataItem) {
$connection->insert('mitacan')
->fields([
'stokkodu' => $dataItem[0],
'logo' => $dataItem[1],
'resim1' => $dataItem[2],
'grupkodu' => $dataItem[3],
'grupadi' => $dataItem[4],
'grupindex' => $dataItem[5],
'paketadedi' => $dataItem[6],
])
->execute();
}
drupal_set_message('Excel verisi veritabanına eklendi.');
}Thank you.