Gather files attachments sent by another PHP script, and output them as a ZIP folder

621

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

53

Answer

Solution:

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();
}
663

Answer

Solution:

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 frommy_script.php?id=123 and saving it into a variable as per this answer (using cURL and settingCURLOPT_BINARYTRANSFER => TRUE)
  • Create a temporary file and writing in it the varaible's data (typicalfopen andfwrite did the job)
  • Add the file to the ZIP archive, as per @DigitalJedi's answer
  • After sending the ZIP, delete the temporary file usingunlink("filename.csv").

People are also looking for solutions to the problem: Refactoring webproject: php mysql javascript

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.