0

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?).

Anis R.
  • 6,656
  • 2
  • 15
  • 37
  • Possible duplicate of [Create a zip file and download it](https://stackoverflow.com/questions/12225964/create-a-zip-file-and-download-it) – Justinas May 18 '19 at 20:28

2 Answers2

1

You could just save all your files in some location, then access them and put them into a zip file like this:

$zip = new ZipArchive;
if ($zip->open('test_new.zip', ZipArchive::CREATE) === TRUE)
{
    // Add files to the zip file
    $zip->addFile('test.txt');
    $zip->addFile('test.pdf');

    // All files are added, so close the zip file.
    $zip->close();
}
DigitalJedi
  • 1,577
  • 1
  • 10
  • 29
0

Well, I rethought my plan, and I finally went for the following:

For each of the IDs:

  • Read the attachment of the http response coming from my_script.php?id=123 and saving it into a variable as per this answer (using cURL and setting CURLOPT_BINARYTRANSFER => TRUE)
  • Create a temporary file and writing in it the varaible's data (typical fopen and fwrite did the job)
  • Add the file to the ZIP archive, as per @DigitalJedi's answer
  • After sending the ZIP, delete the temporary file using unlink("filename.csv").
Anis R.
  • 6,656
  • 2
  • 15
  • 37