Create zip files for download (not same other post)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
irfan798
Forum Newbie
Posts: 11
Joined: Tue Jul 15, 2008 3:45 am
Location: Turkey

Create zip files for download (not same other post)

Post by irfan798 »

Let me explain it. :!:
I want to create zip file for download but i dont want to save it.
Just like this image code

Code: Select all

<?php
header("Content-type: image/png");
$im = @imagecreate(110, 20)
    or die("Cannot Initialize new GD image stream");
$background_color = imagecolorallocate($im, 0, 0, 0);
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5,  "A Simple Text String", $text_color);
imagepng($im);
imagedestroy($im);
?>
?t isn't save image just give it user
But if i write something like that

Code: Select all

 
<?php
 
header("Content-type: application/octet-stream");
header("Content-disposition: attachment; filename=test.zip");
 
$archivename = "test.zip";
$zipobject = new ZipArchive();
$zipobject->open($archivename, ZipArchive::CREATE);
$zipobject->addFile("a.php");
$zipobject->close();
 
 
?>
?t saves the zip file :?
I dont want to save file
Just give it user and destroy itself

You can write your alternate ideas
Thanks for your answers...
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Create zip files for download (not same other post)

Post by alex.barylski »

If it saves the Zip file it's because the methods your calling on the object are saving the file...setting the headers has nothing to do with that.

Look into the API again and see if there isn't a method to initialize the Zip object *without* saving the file...if not...just unlink the file after your finished. ;)
irfan798
Forum Newbie
Posts: 11
Joined: Tue Jul 15, 2008 3:45 am
Location: Turkey

Re: Create zip files for download (not same other post)

Post by irfan798 »

I couldn't find give it without save and can you look it maybe i dont understood it because my english

another thing:

I think to unlink but how can php understand the download finishes :?:
Maybe you say apache log but if download not finish, zip file stand there forever
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Create zip files for download (not same other post)

Post by alex.barylski »

I think to unlink but how can php understand the download finishes
Hmmm...good point.

Ummm...well...you could maybe use the register_shutdown_function() to invoke a callback which would then unlink the file.

What Zip class are you using exactly?
irfan798
Forum Newbie
Posts: 11
Joined: Tue Jul 15, 2008 3:45 am
Location: Turkey

Re: Create zip files for download (not same other post)

Post by irfan798 »

I think there is just one zip class "ZipArchive" am i wrong?

And i am looking register_shutdown_function() now
I don't know it well if you know can you write an example please :oops:
Otherwise i'm searhing it
irfan798
Forum Newbie
Posts: 11
Joined: Tue Jul 15, 2008 3:45 am
Location: Turkey

Re: Create zip files for download (not same other post)

Post by irfan798 »

Okey i read its examples
The register_shutdown_function it calls a function when the current script ends.
But after my script finish the zip file saved and if i use register_shutdown_function whit unlink, the zip file delete instantly. There is no time for user to download the zip file. :(

Please write your ideas and alternate ideas...
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Create zip files for download (not same other post)

Post by Benjamin »

You can unlink it before the download has completed (but not before the download was started) if you're using apache. Why not just save it to the /tmp directory and have linux clean it up.
irfan798
Forum Newbie
Posts: 11
Joined: Tue Jul 15, 2008 3:45 am
Location: Turkey

Re: Create zip files for download (not same other post)

Post by irfan798 »

I can save it to temp but the fact is linux delete files when computer restart and server nearly never be restarted so ? can't do it. And if can there is another point: :arrow:

Code: Select all

<?php
$archivename = "test.zip";
$zipobject = new ZipArchive();
$zipobject->open("/tmp/".$archivename, ZipArchive::CREATE); //Okey i saved it in temp
$zipobject->addFile("a.php");
$zipobject->close();
 
header("Content-type: application/octet-stream");//But how can i give it to users?
?>
 
Like that :?: Not work :!:

Code: Select all

 
header("Content-disposition: attachment; filename=test.zip");
 
Or like that :?: Not work too :!:

Code: Select all

header("Content-disposition: attachment; filename=/tmp/test.zip");
But i think something i will write it when i complete the codes.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Create zip files for download (not same other post)

Post by VladSun »

A very bad solution (eats a lot of resources and depends on memory limits) would be to use file_get_contents after you had created the ZIP file and just before deletion. Then echo the output of file_get_contents.
There are 10 types of people in this world, those who understand binary and those who don't
irfan798
Forum Newbie
Posts: 11
Joined: Tue Jul 15, 2008 3:45 am
Location: Turkey

Re: Create zip files for download (not same other post)

Post by irfan798 »

I write the code just after my post but i wasn't have time to put it there
A very bad solution (eats a lot of resources and depends on memory limits) would be to use file_get_contents after you had created the ZIP file and just before deletion. Then echo the output of file_get_contents.
VladSun can you put an example or can you explain it :?:

Whatever Here is my solution:

Code: Select all

 
<html>
<head>
<meta http-equiv='refresh' content='180'> 
</head>
<body>
<?php
 
///////////////
$minutes = '15';  ///Minute that delete file if this minute past its last modify
$dir_name="/opt/lampp/htdocs/try/tmp"; ///dir name tou want to use like temp !and dont but "/" to end!
//////////////
 
$dir = opendir($dir_name);
while ( gettype ( $filename2 = readdir($dir)) != boolean ) 
{
    $filename=$dir_name."/".$filename2;
    if (is_dir($filename)) {continue;}
        
echo "$filename was change: " . date ("i", filemtime($filename))."<br>"; ///You can delete this line
echo "And now: ".date ("i")."<br>"; ///You can delete this line
        if(date("i")-date("i", filemtime($filename)) >= $minutes or date("i") < date("i", filemtime($filename)))
        {
            if(unlink($filename)){echo $filename." deleted<br>";} else {echo "There is some eror and ".$filename." could not be deleted!";}
        }
 
}
 
?>
</body>
</html>
You can change <meta http-equiv='refresh' content='180'> 180 to what second you want the script work. Its looping

And i didn't work so much on it if you did better than that or if you have better idea :idea: please say it
irfan798
Forum Newbie
Posts: 11
Joined: Tue Jul 15, 2008 3:45 am
Location: Turkey

Re: Create zip files for download (not same other post)

Post by irfan798 »

Hahaha :lol:

This was enough for my work :lol:

Code: Select all

<?php
 
 
header("Content-type: application/zip");
 
header('Content-Disposition: attachment; filename="downloaded.zip"');
 
 
$archivename = "test.zip";
$zipobject = new ZipArchive();
$zipobject->open("/opt/lampp/htdocs/psmatlib/".$archivename, ZipArchive::CREATE);
$zipobject->addFile("a.php");
$zipobject->close();
echo file_get_contents("http://localhost/psmatlib/test.zip");
 
unlink("/opt/lampp/htdocs/psmatlib/".$archivename);
 
 
 
?>
I waste my time Why did't you said it before VladSun
But maybe some one use my tmp script :)

!Solved!
Post Reply