Laravel-excel: [BUG] Temp file is an Invalid Spreadsheet file error

Created on 22 May 2020  路  11Comments  路  Source: Maatwebsite/Laravel-Excel

Prerequisites

  • [x] Checked if your Laravel Excel version is still supported: https://docs.laravel-excel.com/3.1/getting-started/support.html#supported-versions
  • [x] Able to reproduce the behaviour outside of your code, the problem is isolated to Laravel Excel.
  • [x] Checked that your issue isn't already filed.
  • [x] Checked if no PR was submitted that fixes this problem.
  • [x] Filled in the entire issue template

Versions

  • PHP version: 7.3
  • Laravel version: 7.12
  • Package version: 3.1.19

Description

When importing a csv, this error is shown:

/private/var/tmp/laravel-excel-c7dP24qYpZ3MV15ecNHEfR3mD7FoaAxT is an Invalid Spreadsheet file.

Thrown by: /phpoffice/phpspreadsheet/src/PhpSpreadsheet/Reader/Csv.php:350

Steps to Reproduce

When I upload this Excel file:
test.csv.zip

And execute this line of code:

Excel::toArray(new Import, request()->file('file'));

Expected behavior:

I receive an array, based on the CSV file.

Actual behavior:

An Exception was thrown:

/private/var/tmp/laravel-excel-c7dP24qYpZ3MV15ecNHEfR3mD7FoaAxT is an Invalid Spreadsheet file.
bug

Most helpful comment

Can you try setting a different temporary folder in the config/excel.php ?

All 11 comments

When opening your file in PhpStorm I get a warning that it's in a wrong encoding. Might be the reason why phpspreadsheet cannot read it.

Yes, I also get a warning in PhpStorm:
File was loaded in the wrong encoding: 'UTF-8'

Change it to another encoding didn't fix it though...

I had the same problem:laravel-excel-Bmkkls0gUO4BCX1alF0if4xNR8E7x3eD.tmp is an Invalid Spreadsheet file.

Can you share your file en code then @woaidakele. With just that message there isn't a lot I can tell you

PhpOffice\PhpSpreadsheet\Reader\Exception: /tmp/laravel-excel-eybp4kYelqVkudOdRZxagLcdaoldeMyh is an Invalid Spreadsheet file. in

I am having this issue too however the issue is only on the server. The import process works just fine locally(MACOS Catalina). So quiet clearly this is an issue with the server configuration but I can not see what is so different that is causing this. I have tried numerous files and including those that work locally just fine. I have also made sure the requirements have been met.

Any ideas @patrickbrouwers what on the server could be missing, I would be truly grateful as I have spent quite a lot fo time on this now? Could it be a permissions issue? I made sure the folders have read and write permissions I don't have any other issues uploading files to the site. I have attached two examples of files I am using.

Archive 2.zip

$import = new EventAttendeeUploadRepository($event, $user); $import->import($request->file('file'), null, \Maatwebsite\Excel\Excel::CSV);

Can you try setting a different temporary folder in the config/excel.php ?

Can you try setting a different temporary folder in the config/excel.php ?

@patrickbrouwers Awesome job mate thanks! I set the path to storage_path() and its worked perfectly! so must be permission issue with sys_get_temp_dir() directory! Thanks!

Glad you managed to solve it 馃憤

I had the same problem, laravel-excel-L3M0GQ527aFbitUJpJjwBcDxsPEbS3T1.xml is an Invalid Spreadsheet file. I set the temporary path to somewhere else but no luck yet.

    /*
    |--------------------------------------------------------------------------
    | Local Temporary Path
    |--------------------------------------------------------------------------
    |
    | When exporting and importing files, we use a temporary file, before
    | storing reading or downloading. Here you can customize that path.
    |
    */
    'local_path'          => public_path('laravel-excel'),

Controller

public function index()
{

    $data = Excel::toArray('', public_path().'/feed.xml', null, \Maatwebsite\Excel\Excel::XML)[0];

    dd($data);
}

Error:
PhpOffice\PhpSpreadsheet\Reader\Exception
C:\xampp\htdocs\bestprices\public\laravel-excel\laravel-excel-taQBiSKsOD31kK94wxbUT8S1MXo32EdZ.xml is an Invalid Spreadsheet file.

Hi
In my case the problem is because the mime_content_type gets it wrong.
The uploaded file lives in /tmp as a file without extension:
/tmp/laravel-excel-JNW2yDj18VXX56WnhAGvB6APvF7eM63N
so it fails the in_array check and then falls back to the mime_content_type($pFilename); line wich believes the file is a application/octet-stream

Csv.php, line 506-534

    public function canRead($pFilename)
    {
        // Check if file exists
        try {
            $this->openFile($pFilename);
        } catch (InvalidArgumentException $e) {
            return false;
        }

        fclose($this->fileHandle);

        // Trust file extension if any
        $extension = strtolower(pathinfo($pFilename, PATHINFO_EXTENSION));
        if (in_array($extension, ['csv', 'tsv'])) {
            return true;
        }

        // Attempt to guess mimetype
        $type = mime_content_type($pFilename);
        $supportedTypes = [
            'application/csv',
            'text/csv',
            'text/plain',
            'inode/x-empty',
        ];

        return in_array($type, $supportedTypes, true);
    }
Was this page helpful?
0 / 5 - 0 ratings

Related issues

disto picture disto  路  3Comments

daraghoshea picture daraghoshea  路  3Comments

alejandri picture alejandri  路  3Comments

vandolphreyes picture vandolphreyes  路  3Comments

amine8ghandi8amine picture amine8ghandi8amine  路  3Comments