Jump to content


Photo

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


  • Please log in to reply
2 replies to this topic

#1 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 19 July 2006 - 12:33 AM

We're writing this file:

$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);

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?

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 19 July 2006 - 12:36 AM

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

http://www.phpclasse...ckage/2088.html

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.

#3 simcoweb

simcoweb
  • Members
  • PipPipPip
  • Advanced Member
  • 1,102 posts
  • LocationCA

Posted 19 July 2006 - 03:04 AM

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.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users