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:
-
Ensure Proper Output Buffering: You are already using
ob_end_clean()andob_start(), which is good practice to clear any previous output buffering. Make sure these are correctly placed before the download call. -
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.
-
Upgrade/Downgrade Packages: Since you mentioned that downgrading
symfony/http-foundationresolves the issue, you might want to check if there are any updates or patches available formaatwebsite/excelthat address compatibility with the newer version ofsymfony/http-foundation. -
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.
-
Check for Known Issues: Look for any known issues or bug reports on the
maatwebsite/excelGitHub repository or thesymfony/http-foundationrepository. There might be others who have encountered the same issue, and there could be a recommended fix or workaround. -
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.