Jump to content


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


How can I write this file & turn it into a downloadable zip file at same time?

Recommended Posts

We're writing this file:

[code]$data = "";
$result = mysql_query("SHOW COLUMNS FROM `iplog`") or die(mysql_error());
while($row = mysql_fetch_array($result)) {
    $data .= $row[0].",";
$data = substr($data,0,-1)."\r\n";

$result = mysql_query("SELECT * FROM `iplog`") or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
    foreach($row as $r) {
        $data .= "$r,";
    $data = substr($data,0,-1)."\r\n";

$handle = fopen("data.csv","wb");

Which works great in creating the CSV file. However, we'd like to make this newly generated file downloadable. As it stands right now all i've been able to do is have it display the CSV file and the person would need to copy and paste it. Sorta lame i'd say. So, what I would like it to do is either:

1) write/create the file AND turn it into a downloadable zip file, or
2) write/create the file AND have another 'option' to download it which would then turn it into a zip file (so instead of write/zip at once it's two different functions).

With scenario #2 the person could then decide if they want to copy and paste or zip and download.


Share this post

Link to post
Share on other sites
As far as zipping the file you may want to use a script like this:


Offering the file for a download is as easy as changing the headers to send the file rather than display it:

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;

Where size, type, name, and contents should be fairly obvious.

Share this post

Link to post
Share on other sites
That class file was a snap and worked exactly as I needed. Thanks for pointing me to that great resource. That's definitely a class file that can be used a LOT.

Share this post

Link to post
Share on other sites


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.