How to create a file on the server without saving it, just giving the user the possibility to open it or save it on the local machine?
First, create the file contents as you would do normally. Then add the header attributes in which we specify the type of file.
Last, echo the contents of the file, which will take the header attributes and ask the user for saving the file.
Example creating a CSV file for download:
$fileContent = ”;
$fileContent .= “Id,First name,Last name\n”;
$fileContent .= “1,Jack,Doe\n”;
$fileContent .= “2,Jill,Jackson\n”;
header(“Content-Disposition: attachment; filename=”output.csv”);