1

I have a function for export one csv file include all answers of each survey. I need one function for export all answers of all surveys (one survey per one csv file) into menory and zip it before download to computer

Not save all csv file into server or local storage, I need storage it into memory and zip it before download to computer

I had use this code:

function exportAllAnswer() {
    allTokenId = getAllTokenId();

    foreach(allTokend as $key->$value) {
        exportAnswer($value->id, false);
    }
    $path = 'exportAllCsv';

    // we deliver a zip file
    header("Content-Type: archive/zip");

    // filename for the browser to save the zip file
    header("Content-Disposition: attachment; filename=allCsvs.zip");

    // get a tmp name for the .zip
    $tmp_zip = tempnam ("tmp", "tempname") . ".zip";

    //change directory so the zip file doesnt have a tree structure in it.
    chdir($path);
    // zip the stuff (dir and all in there) into the tmp_zip file
    exec('zip '.$tmp_zip.' *');

    // deliver the zip file
    $fp = fopen("$tmp_zip","r");
    echo fpassthru($fp);

    // clean up the tmp zip file
    unlink($tmp_zip);

    // delete folder csv
    rmdir($path);
}
Passerby
  • 9,715
  • 2
  • 33
  • 50
  • 2
    seems to be a duplicate of http://stackoverflow.com/questions/1189019/manipulate-an-archive-in-memory-with-php-without-creating-a-temporary-file-on-d It seems to work very well. good luck ! – MatRt Feb 25 '13 at 04:11
  • No, I need how to storage the exported file into memory and how to zip it? – Amida Nguyen Feb 25 '13 at 04:24
  • When creating zip files in memory you can run into memory limit issues...which is not to say you shouldn't do it in your case, it just depends on how large the CSV file might get. – Matt Browne Feb 25 '13 at 04:36
  • You may also archive this using [`ZipArchive::addFromString`](http://www.php.net/manual/en/ziparchive.addfromstring.php). – Passerby Feb 25 '13 at 04:36
  • Dear Amida, I give you a link talking about ZIP file in memory, not in a file on local storage. This seems to be exactly what you need. Please read completely this link before saying "No". With ZipFile, you can create a zip in memory, add content as file, zip all thing and send to user... – MatRt Feb 25 '13 at 04:39
  • I can not use external libary, please help me how to put some csv file into memory and zip it? – Amida Nguyen Feb 25 '13 at 07:49

0 Answers0