0

I am using PHP Spreadsheet to read an excel file

$inputFileName = '/usaid/Files/Installation_Report.xlsx';
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();

try
{
    /** Load $inputFileName to a Spreadsheet Object  **/
    $spreadsheet = $reader->load($inputFileName);

    print_r($spreadsheet);
    exit();
} catch (\Exception $exception) {
    print_r($exception->getMessage() . $exception->getFile());
    exit();
}

But I am getting below error

File "/usaid/Files/Installation_ Report.xlsx" does not exist.F:\xampp\htdocs\usaid\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Shared\File.php

The path is

F:\xampp\htdocs\usaid\Files

Update 1

As per this answer I have tried to change the code

 public function actionRun()
{

    $inputFileName = 'usaid/Files/Installation_Report.xlsx';

    /**  Identify the type of $inputFileName  **/
    $inputFileType = \PhpOffice\PhpSpreadsheet\IOFactory::identify($inputFileName);

    try
    {

        /**  Create a new Reader of the type that has been identified  **/
        $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);

        /**  Load $inputFileName to a Spreadsheet Object  **/
        $spreadsheet = $reader->load($inputFileName);

        /**  Convert Spreadsheet Object to an Array for ease of use  **/
        $schdeules = $spreadsheet->getActiveSheet()->toArray();
        //$reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);

        var_dump($schdeules);
        die();
    }
    catch (\Exception $exception)
    {
        print_r($exception->getMessage() . $exception->getFile());
        exit();
    }


}

Now I am getting

enter image description here

InvalidArgumentException File "usaid/Files/Installation_Report.xlsx" does not exist.

Note: Tried even with $inputFileName = Yii::$app->basePath.'\Files\Installation_Report.xlsx'; but same result

Olivier
  • 13,283
  • 1
  • 8
  • 24
Moeez
  • 494
  • 9
  • 55
  • 147
  • did you tried to update your composer ?! and did you check to file is exist or not ?! – ttrasn Nov 19 '20 at 08:49
  • @ttrasn yes I have updated the composer and yes the file is physically present in the folder – Moeez Nov 19 '20 at 09:41
  • @ttrasn the path is `F:\xampp\htdocs\usaid\Files` – Moeez Nov 19 '20 at 10:01
  • then remove the leading backslash so try `$inputFileName = 'usaid/Files/Installation_Report.xlsx';` – RiggsFolly Nov 19 '20 at 14:50
  • @RiggsFolly tried this but same result – Moeez Nov 19 '20 at 17:23
  • 1
    Do `$inputFileName = 'F:\xampp\htdocs\usaid\Files\Installation_Report.xlsx'` – hostingutilities.com Nov 19 '20 at 20:32
  • @wp-overwatch.com already tried but same result :( – Moeez Nov 20 '20 at 04:20
  • Double-check that the file exists in that folder with that name. There is no reason that `is_file('F:\xampp\htdocs\usaid\Files\Installation_Report.xlsx')` returns `false` if the file exists. And all this has nothing to do with Yii, PhpSpreadsheet or whatever; it's just basic file access. – Olivier Nov 21 '20 at 08:34
  • @Olivier I have triple-checked it the file does exists.. – Moeez Nov 21 '20 at 13:03
  • What does `is_dir('F:\xampp\htdocs\usaid\Files')` return? – Olivier Nov 21 '20 at 13:38
  • @Olivier it returns `bool(true)` – Moeez Nov 23 '20 at 04:48
  • What does `glob('F:\xampp\htdocs\usaid\Files\*.*')` return? – Olivier Nov 23 '20 at 08:40
  • it returns me `array(5) { [0]=> string(40) "F:\xampp\htdocs\usaid\Files\Example.xlsx" [1]=> string(52) "F:\xampp\htdocs\usaid\Files\Installation_Report.xlsx" [2]=> string(49) "F:\xampp\htdocs\usaid\Files\Meter_Acceptance.xlsx" [3]=> string(56) "F:\xampp\htdocs\usaid\Files\Meter_Handover_Document.xlsx" [4]=> string(36) "F:\xampp\htdocs\usaid\Files\logo.jpg" }` – Moeez Nov 23 '20 at 12:42
  • That's strange. What does `filesize('F:\xampp\htdocs\usaid\Files\Installation_Report.xlsx')` return? And did you try with the other files in that directory? – Olivier Nov 23 '20 at 13:19
  • @Olivier it gives me `int(16974)` and yes I have tried other files as well and same result is encountered – Moeez Nov 24 '20 at 03:27
  • Then the file is found. Are you really sure that `is_file('F:\xampp\htdocs\usaid\Files\Installation_Report.xlsx')` returns `false`? – Olivier Nov 24 '20 at 11:40
  • Is F: a local drive or a letter mapped to a network share? – Olivier Nov 24 '20 at 12:47
  • @Olivier yes it's a local drive – Moeez Nov 25 '20 at 03:21

1 Answers1

1
realpath(Yii::$app->basePath).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'Files'.DIRECTORY_SEPARATOR.'Installation_Report.xlsx';
  • use the constant DIRECTORY_SEPARATOR instead of '/'.
  • the result should be : "F:\xampp\htdocs\usaid\Files\Installation_Report.xlsx"
Nicoweb
  • 125
  • 2