I currently have a PHP script that exports data about an invoice in a CSV file, based on the invoice's ID number. The file is dynamically generated and is not saved on the server. To be more specific, I am echoing rows, and setting the following headers so that the echos appear in a file attachment:
header('Content-type: text/plain');
header("Content-Disposition: attachment; filename=INV${id}.csv");
The way I can call the script is using:
my_script.php?id=123
However now, I need to write a script that grabs all invoices of a given day, get their corresponding CSV files, and download them to the client as a ZIP file. Can I do that using my already existing script that outputs one file?
What I was thinking about is getting the list of all relevant invoice IDs from the database (say 123, 456 and 789), then looping over them and call my script for each of the IDs, like:
my_script.php?id=123 //then 456, then 789
and then save the file results into an array.
My attempt so far:
So far, I tried saving one resulting file as a variable as per this answer, but the variable remains empty (while the already existing script does return a non-empty file):
$file = getScriptOutput("my_script.php?id=123");
echo $file;
function getScriptOutput($path, $print = FALSE)
{
ob_start();
if( is_readable($path) && $path )
{
include $path;
}
else
{
return FALSE;
}
if( $print == FALSE )
return ob_get_clean();
else
echo ob_get_clean();
}
So, two questions here:
1 - How do I make an array of files, where each one is a file result (attachment) of a PHP script?
2 - How do I combine all these dynamic files into one ZIP file in PHP?
Edit: The suggested duplicate does help for the zipping part, but I need to know how to get the file attachment of a PHP script (Is there a way without saving the file on the server, because of storage and security concerns?).