Page 1 of 1

Jacek's Create Zip files tutorial

Posted: Mon Nov 12, 2012 9:55 am
by Fidbeck
I'm so noob in this that I didn't even know it was possible to create zip files in a PHP file lol

but I have a question.
This is one part of the code, the one that interests me, for now
[syntax=php]$zip = new ZipArchive();

$zip->open('example.zip', ZipArchive::CREATE);

$files = scandir('uploads');
unset($files[0], $files[1]);
print_r($files)[/syntax]

Jacek has only one folder with 4 images inside
But if I have more than one folder?
How am I going to choose?

Another question
How can I change the name? instead of example, I want another nice, like, the name of the folder, the thing here is to get the file name.
Or change it to a random name
for that I think I have to define the charset, and use rand() function, but not sure where to use it

Re: Jacek's Create Zip files tutorial

Posted: Mon Nov 12, 2012 1:44 pm
by Temor
I'm not sure I understand completely.
You're saying you have more than one folder, but not what kind of folders. Why do you need more than one, and where are these other folders located?

You can rename it easily. I don't see where the problem is.
[syntax=php]$zip->open('anyNameHereWillWork.zip', ZipArchive::CREATE);[/syntax]

[syntax=php]
$filename = $_GET['file_name'];
$zip->open($filename, ZipArchive::CREATE);[/syntax]


there are a million ways to give the file a unique name.

if you wish to give it a random string as the name, you can use this:
[syntax=php] $charset = array_flip(array_merge(range('a','z'), range('A','Z'), range(0, 9)));
$filename = implode('', array_rand($charset,15));
$zip->open($filename, ZipArchive::CREATE);[/syntax]

Re: Jacek's Create Zip files tutorial

Posted: Mon Nov 12, 2012 2:42 pm
by Fidbeck
Jacek in the toturial is giving a fixed name, to it, and I want to grab the name of the folder and save it.
is the file is ahgZkjf it will save as ahgZkjf.zip and if the filename is myfoldernamerocks it will save as myfoldernamerocks.zip

I'll try that one out :)
Thanks