0

How do I pass multiple csv files from sql statements via http request? Writing the files to the server to zip may not be an option for me.

At this point my code generates one csv file.

function exportFile($plan) {
$query = "SELECT * from table WHERE name = '${plan}'";

$result = mysqli_query($connection, $query);
if (!$result) {
    die("Query Failed during export of batch file: " . mysqli_error($connection));
} else {

    $dateNow = date("Y-m-d_His");
    $fileName =  $plan . '_' . $dateNow . ".csv";
    header('Content-Type: text/csv; charset=utf-8');
    header("Content-Disposition: attachment; filename*=UTF-8''" . rawurlencode($fileName));

    $output = fopen('php://output', 'w');
    $header1 = array(1, 2, 3, 4, 5, 6, 7, 8);       
    $header2 = array(1, 2, 3, 4, 5, 6, 7, 8);

    fputs($output, implode($header1, ',') . "\n");
    fputs($output, implode($header2, ',') . "\n");
    fputs($output, implode($header3, ',') . "\n");

    while ($row = mysqli_fetch_row($result)) {
        fputs($output, implode($row, ',') . "\n");
    }
    fclose($output);
    exit();
}    
}
tai
  • 39
  • 1
  • 8
  • Which part are you struggling with? 1) Generating multiple csv files? 2) Zipping them together? 3) Sending a zip file? Because each of them could easily be a separate question. To me this question is way too broad as it stands. – Shadow Apr 07 '16 at 16:39
  • apologies. Edited for clarification. If I can't write the files on to the server to zip, how do I zip and send the generated files on the fly? – tai Apr 07 '16 at 16:48
  • You may have a memory issue if you want to assemble the whole file in memory. – Shadow Apr 07 '16 at 16:54
  • Possible duplicate of [Create a zip file using PHP class ZipArchive without writing the file to disk?](http://stackoverflow.com/questions/4165289/create-a-zip-file-using-php-class-ziparchive-without-writing-the-file-to-disk) – Shadow Apr 07 '16 at 16:54
  • I selected the above topic as duplicate and not the one it links to as duplicate because that original one does save it to disk. Alternatively, you can try: http://stackoverflow.com/questions/1189019/manipulate-an-archive-in-memory-with-php-without-creating-a-temporary-file-on-d – Shadow Apr 07 '16 at 16:56
  • thanks for the link. Is it possible to get a clarification on this line: //add files to the zip, passing file contents, not actual files $zip->addFile($file_content, $file_name); "file content, not actual files" – tai Apr 07 '16 at 17:15
  • Can you be specific where you found that piece of code? – Shadow Apr 07 '16 at 17:38
  • in the marked answer of the 2nd link you provided [link]http://stackoverflow.com/questions/1189019/manipulate-an-archive-in-memory-with-php-without-creating-a-temporary-file-on-d – tai Apr 07 '16 at 17:44
  • Since it is a custom library, no a standard one, I cannot offer any more guidance than that is provided in the comment. The 1st parameter is the content of the file (csv content for you), the 2nd is probably the name of the file that appears in the archive. But it would be soooo much simpler to test it yourself to see how it works, would not it? But I would double check the source just to make sure it does use the memory and not disk space. – Shadow Apr 07 '16 at 18:43
  • lol yes. I am mobile at the moment and have been trying to find readings on it. Thank you very much. – tai Apr 07 '16 at 18:45

0 Answers0