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

auxit's avatar
Level 4

symfony/http-foundation unlink bug with Laravel-Excel

Hello there. I hope I'm using this forum appropriately. Let me know if this should go somewhere else or if I should be providing more information.

I am using the maatwebsite/excel library and I noticed it suddenly stopped working after a Laravel upgrade. I get the error below. I can get around this by downgrading the symfony/http-foundation package from 7.2.3 to 7.0.7. I guess something changed with how http-foundation handles temp files.

[2025-02-07 22:20:54] local.ERROR: unlink(/home/vagrant/code/mysite/storage/framework/cache/laravel-excel/laravel-excel-WAx5diO5dxzeoOZjKOjquIxtuiYgMb4z.xlsx): Text file busy {"userId":1,"exception":"[object] (ErrorException(code: 0): unlink(/home/vagrant/code/mysite/storage/framework/cache/laravel-excel/laravel-excel-WAx5diO5dxzeoOZjKOjquIxtuiYgMb4z.xlsx): Text file busy at /home/vagrant/code/mysite/vendor/symfony/http-foundation/BinaryFileResponse.php:352)
[stacktrace]
#0 /home/vagrant/code/mysite/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php(290): Illuminate\\Foundation\\Bootstrap\\HandleExceptions->handleError()
#1 [internal function]: Illuminate\\Foundation\\Bootstrap\\HandleExceptions->Illuminate\\Foundation\\Bootstrap\\{closure}()
#2 /home/vagrant/code/mysite/vendor/symfony/http-foundation/BinaryFileResponse.php(352): unlink()
#3 /home/vagrant/code/mysite/vendor/symfony/http-foundation/Response.php(395): Symfony\\Component\\HttpFoundation\\BinaryFileResponse->sendContent()
#4 /home/vagrant/code/mysite/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(1193): Symfony\\Component\\HttpFoundation\\Response->send()
#5 /home/vagrant/code/mysite/public/index.php(17): Illuminate\\Foundation\\Application->handleRequest()
#6 {main}
"}

I simplified my code so that I have an Exports\ReportExport.php file like this:

<?php

namespace App\Exports;

// use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\FromArray;

class ReportExport implements FromArray
{

    public function __construct(array $data)
    {
        $this->data = $data;
    }

    public function array(): array
    {
        return $this->data;
    }
}

And a controller action that does this:

        $results = [['a','b','c']];
        $export = new ReportExport($results);
        ob_end_clean();
        ob_start();
        return Excel::download($export, 'report.xlsx'); 
0 likes
1 reply
LaryAI's avatar
Level 58

The issue you're encountering seems to be related to how the symfony/http-foundation package handles temporary files in newer versions. The error message indicates that the file is still being used when the unlink function is called, which can happen if the file is not properly closed or if there's a delay in releasing the file handle.

Here are a few steps you can take to resolve this issue:

  1. Ensure Proper Output Buffering: You are already using ob_end_clean() and ob_start(), which is good practice to clear any previous output buffering. Make sure these are correctly placed before the download call.

  2. Check for Open File Handles: Ensure that there are no open file handles or streams that might be holding onto the file. This can sometimes happen if the file is being accessed elsewhere in your code.

  3. Upgrade/Downgrade Packages: Since you mentioned that downgrading symfony/http-foundation resolves the issue, you might want to check if there are any updates or patches available for maatwebsite/excel that address compatibility with the newer version of symfony/http-foundation.

  4. Use Temporary Files Carefully: If the issue persists, consider using a different approach to handle temporary files. You can manually manage the creation and deletion of temporary files to ensure they are not being accessed when you attempt to delete them.

  5. Check for Known Issues: Look for any known issues or bug reports on the maatwebsite/excel GitHub repository or the symfony/http-foundation repository. There might be others who have encountered the same issue, and there could be a recommended fix or workaround.

  6. Alternative File Handling: If the problem is persistent, you might want to try using a different method to handle file downloads, such as streaming the file directly to the response without saving it to a temporary location.

Here's a revised version of your controller action that ensures output buffering is handled correctly:

use Maatwebsite\Excel\Facades\Excel;
use App\Exports\ReportExport;

public function downloadReport()
{
    $results = [['a', 'b', 'c']];
    $export = new ReportExport($results);

    // Clear any previous output buffering
    if (ob_get_length()) {
        ob_end_clean();
    }

    // Start output buffering
    ob_start();

    // Return the Excel download response
    return Excel::download($export, 'report.xlsx');
}

If none of these solutions work, consider reaching out to the maintainers of the maatwebsite/excel package or checking their documentation for any updates related to this issue.

Please or to participate in this conversation.