Cannot pass variable filename
duckboy81 opened this issue · 6 comments
It appears there is no option to pass a variable filename to the getAdditionalHeaders()
function.
If I understand correctly, you want that function to accept a file name different from the one set in constructor? If that is truly what you want, next question will be, why?
So am I right in thinking that you have something roughly along the lines of:
$resource = new DatabaseFileResource($fileId);
// ...where DatabaseFileResource is a custom Resource implementation
// which is pulling data from a database
...and what you want is to be able to have the browser get the file with the original name as stored in the database?
You got it!
In order to specify the file name you just need to include it as part of the content-disposition header returned by getAdditionalHeaders()
.
For example, the implementation from the default FileResource
looks like this:
public function getAdditionalHeaders(): array
{
return [
'Content-Disposition' => 'attachment; filename="' . \basename($this->localPath) . '"'
];
}
You simply need to replace \basename($this->localPath)
with the file name you have retrieved from the database.
Closing this as I believe the above info should resolve your issue, please re-open if you need more assistance.