0

My goal is to let the user upload a xlsx-file and then read the file to create objects. So i created a file upload page in my custom module where the user can upload a file via the 'file' form field and the read the file via PHPExcel (which was already present when i started working on the module so i used this instead of PHPSpreadsheet).

The problem is, that after uploading the file and loading it via PHPExcel, i get several errors:

ZipArchive::getFromName(): Invalid or uninitialized Zip object in PHPExcel_Reader_Excel2007->_getFromZipArchive()

ZipArchive::close(): Invalid or uninitialized Zip object in PHPExcel_Reader_Excel2007->load()

Trying to get property 'Relationship' of non-object in PHPExcel_Reader_Excel2007->load()

When reading the file directly from a folder via PHPExcel it works (with the same file) and the uploaded xlsx file opens without problems in Excel 2016.

After reseraching i suspect that this has something to do with file permissions and the uploaded file indeeddiffers from the original file in the permissions:

In the security tab of the uploaded file an entry "Authentificated Users" was added.

My question is: What causes my problems? Am i on the right ttrack? Why does this permission entry gets added? How can i stop the uploader to do this?

My code:

$form['file'] = [
    '#type' => 'file',
    '#title' => t('Choose a xlsx-file based on the template.'),
  ];

and:

  // Process the file provided by the user.
  $form_file = $form_state['values']['file'];

  $file_destination = 'public://';

  $validators = ['file_validate_extensions' => ['xlsx']];

  $file = file_save_upload('file', $validators, $file_destination, FILE_EXISTS_REPLACE);

  if (!$file) {
    watchdog('my_module', 'PHPExcel could not import! Import file missing.');
    return NULL;
  }

  $file_name = $file->filename;
  $import_file = $file_destination.$file_name;

  // Load the library and initialize.
  module_load_include('php', 'my_module', 'lib/phpexcel_1.8.0/PHPExcel');

  // Load the import file.
  try {
    $objReader = PHPExcel_IOFactory::createReader('Excel2007');
    $objPHPExcel = $objReader->load($import_file);
  }

  catch (Exception $exception) {
    drupal_set_message($exception);
    watchdog('rdp_cellmodel', $exception);
  }

  // read file etc.
2
  • 2
    PHPExcel is very old and shouldn't be used. But if it suffers from the same problem as its successor does, then your issue is likely that the library simply doesn't support file URIs. Pass a full local path to the reader instead
    – Clive
    Commented Jun 27, 2019 at 13:13
  • Yeah, i really should migrate to PHPSpreadsheet. Your hint worked tho. The problem was, that i didn't gave PHPExcel the full local path. Thanks.
    – Xyarvius
    Commented Jun 27, 2019 at 13:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.