Jump to content

Writing array to ZIP file


smidgen11

Recommended Posts

HI,

 

I have some code that saves a zip file to a directory that contains a csv file. The code I currently have, successfully creates a zip file containing the csv file I want to create. The issue is, the only record that gets written to the csv file inside the ZIP is the last record of the array. I know this is probably something very simple but I am having some trouble with it. Ive been google'ing but to no avail. Here is my code:

 


<?php include("sqlconnect.php");?>	
<?php
  $sql="select Username as 'ALIAS', Extension as 'DTMF_ACCESS_ID' from $tbl_entering";
  $result=mysql_query($sql);

  $filename = 'entering.VMAIL';
  $filename = $filename."_".date("Y-m-d_H-i",time());
  $header="ALIAS,DTMF_ACCESS_ID\n";

    while($rows=mysql_fetch_array($result))
  {					 
        $data="$rows[0],$rows[1]\n";
  }



  $csvdata= $header.$data;
  $zipname = 'Batch_'.date("Y-m-d_H",time());

  $zip = new ZipArchive;
  $res = $zip->open("./batch/$zipname.zip", ZipArchive::CREATE);
  if ($res === TRUE) {
    $zip->addFromString("$filename.csv", $csvdata);
    $zip->close();
    echo 'ok';
      } else {
        echo 'failed';
      }
?>

 

Thanks for the help.

Link to comment
https://forums.phpfreaks.com/topic/238807-writing-array-to-zip-file/
Share on other sites

You overwrite $data each time through the loop so on the last iteration it contains only the last row.  Try this:

 

$data .= "$rows[0],$rows[1]\n";

 

And you probably need this before the loop so that you don't generate a notice the first time through:

 

$data = "";

 

Or shorter:

 

$csvdata = "ALIAS,DTMF_ACCESS_ID\n";

    while($rows=mysql_fetch_array($result))
  {					 
        $csvdata .= "$rows[0],$rows[1]\n";
  }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.