Resolved - PhpSpreadsheet failed to open file for writting

63 Views Asked by At

I'm encountering an issue with the PhpSpreadsheet library. From a URL, I'm attempting to export the database to an Excel file and have it directly downloaded. However, when I visit the URL, I encounter the following errors:

Error 1:

Warning: fopen(ode_export_data202402163327.xlsx): Failed to open stream: Permission denied in /var/www/ode.od-sites.fr/wp-content/plugins/ode-code-download/includes/exportDataToExcel/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/BaseWriter.php on line 117

Error 2:

Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Writer\Exception: Could not open file "ode_export_data202402163327.xlsx" for writing. in /var/www/ode.od-sites.fr/wp-content/plugins/ode-code-download/includes/exportDataToExcel/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/BaseWriter.php on line 119
( ! ) PhpOffice\PhpSpreadsheet\Writer\Exception: Could not open file "ode_export_data202402163327.xlsx" for writing. in /var/www/ode.od-sites.fr/wp-content/plugins/ode-code-download/includes/exportDataToExcel/vendor/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Writer/BaseWriter.php on line 119

I have checked the associated permissions, and here is what I found in the console:

The root directory of my plugin:

drwxrwxr-x 3 vivian vivian 4096 Feb 6 09:48 ode-code-download/

The directory where my files related to the export feature are located:

drwxr-xr-x 3 www-data www-data 4096 Feb 16 12:51 exportDataToExcel/

The files in the export feature directory:

-rw-rw-r-- 1 www-data www-data 2039 Feb 16 15:33 export-data-to-excel-functions.php
-rw-rw-r-- 1 www-data www-data 150 Feb 16 15:00 export-data-to-excel.php
drwxrwxr-x 9 www-data www-data 4096 Feb 16 12:32 vendor/

And the specific file in the vendor directory:

-rw-rw-r-- 1 www-data www-data 3599 Jan 24 11:41 BaseWriter.php

The permissions seem correct. I am stuck, and I don't understand why my permissions are not being recognized.

Here is the code for the function that exports the data:

<?php

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

add_action('wp_loaded', 'handle_export_action');

function handle_export_action()
{
  if (isset($_GET['page']) && $_GET['page'] === 'ode_code_download_page' && isset($_GET['action']) && $_GET['action'] === 'export-data') {
    ode_cd_export_to_excel();
    wp_redirect(admin_url('admin.php?page=ode_code_download_page&download-status=success'));
  }
}

function ode_cd_export_to_excel()
{
  global $wpdb;
  $table_name = $wpdb->prefix . 'ode_download_code';

  $results = $wpdb->get_results("SELECT * FROM $table_name", ARRAY_A);

  if ($results) {
    $spreadsheet = new Spreadsheet();

    $sheet = $spreadsheet->getActiveSheet();
    $sheet->setCellValue('A1', 'ID');
    $sheet->setCellValue('B1', 'unique_code');
    $sheet->setCellValue('C1', 'downloadable_product_id');
    $sheet->setCellValue('D1', 'remaining_uses');
    $sheet->setCellValue('E1', 'created_at');

    $row = 2;
    foreach ($results as $result) {
      $sheet->setCellValue('A' . $row, $result['ID']);
      $sheet->setCellValue('B' . $row, $result['unique_code']);
      $sheet->setCellValue('C' . $row, $result['downloadable_product_id']);
      $sheet->setCellValue('D' . $row, $result['remaining_uses']);
      $sheet->setCellValue('E' . $row, $result['created_at']);
    }

    $writer = new Xlsx($spreadsheet);

    $filename = 'ode_export_data' . date('Ymdis') . '.xlsx';

    $writer->save($filename);
    readfile($filename);
    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Content-Disposition: attachment;filename="' . $filename . '"');
    header('Cache-Control: max-age=0');
    unlink($filename);
    exit;
  } else {
    wp_redirect(admin_url('admin.php?page=ode_code_download_page&download-status=error'));
    exit;
  }
}

I'm not sure what to do if anyone has any clues to help me.

0

There are 0 best solutions below