Jump to content

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


simcoweb

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");
fwrite($handle,$data);
fclose($handle);[/code]

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.

Ideas?
Link to comment
Share on other sites

As far as zipping the file you may want to use a script like this:

http://www.phpclasses.org/browse/package/2088.html

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

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

Where size, type, name, and contents should be fairly obvious.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.