smidgen11 Posted June 8, 2011 Share Posted June 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/238807-writing-array-to-zip-file/ Share on other sites More sharing options...
AbraCadaver Posted June 8, 2011 Share Posted June 8, 2011 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"; } Quote Link to comment https://forums.phpfreaks.com/topic/238807-writing-array-to-zip-file/#findComment-1227085 Share on other sites More sharing options...
smidgen11 Posted June 8, 2011 Author Share Posted June 8, 2011 Thanks alot. That did it. Quote Link to comment https://forums.phpfreaks.com/topic/238807-writing-array-to-zip-file/#findComment-1227114 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.