Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

carina312's avatar

download excel-file

Hi guy's,

I want to download an excel-file stored in storage/app/private/import via controller-function but everytime i open the downloaded-file I get an error: 'File-Type does not match file-extension'.

public function download($path) { $disk = Storage::disk('local'); return Storage::download($path); }

I have this problem with .xls and .xlsx Files

Can you help me?

0 likes
8 replies
Glukinho's avatar

everytime i open the downloaded-file I get an error

Where is this error? In a browser, in Excel? Can you give a screenshot?

carina312's avatar

The error happens when opening the file in Excel.

„Excel cannot open the file 'corrupted.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.“

Glukinho's avatar

If you get this file right from the storage (using SCP for example) is it opened fine?

I mean, is this file corrupted from the time of creation or is it corrupted during downloading? These are two different cases.

carina312's avatar

When I open the file directly on the server, everything works fine. So, the problem occurs during download.

Glukinho's avatar

Look in browser's dev tools which content-type header is used for download response. Maybe there is something wrong and browser breaks a file.

Try to add explicit content type:

$content_type = match(true) {
	Str::endsWith($path, '.xls')  => 'application/vnd.ms-excel',
	Str::endsWith($path, '.xlsx') => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
};

return Storage::download($path, basename($path), [
	'Content-type' => $content_type,
]);

Can you give both normal and broken file to compare?

Glukinho's avatar

Also there are bug reports describing the same behaviour as yours: https://github.com/laravel/framework/issues/56209

Which Laravel version you use?

I confirm XLSX file is downloaded fine on Laravel 12.50.0 + php artisan serve:

// routes/web.php

Route::get('test', function() {
    return Storage::download('sample.xlsx');
});

http://127.0.0.1:8000/test gives me normal and readable xlsx file.

carina312's avatar

Response of the browser: content-type: application/vnd.ms-excel

In my test-file there is only the word TEST

and in the broken file there are many symbols, numbers,...:

Calibri Light1,6¼)Calibri16¼)Calibri1Ü6¼)Calibri1ܐ)Calibri1ܐ)Calibri1Ü<)Calibri1Ü>)Calibri1Ü? 00_ ;_ *}-} 00_ ;_ *}-} 00_ ;_ *}-} 00_ ;_ *}-} 00_ ;_ *}-} 00_ ;_ *}-} 00_ ;_ *}-}

Glukinho's avatar

Can you share real files? Normal and downloaded broken one.

Please or to participate in this conversation.