Jacek's Create Zip files tutorial

Ask about a PHP problem here.
Post Reply
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Jacek's Create Zip files tutorial

Post 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
User avatar
Temor
Posts: 1186
Joined: Thu May 05, 2011 8:04 pm

Re: Jacek's Create Zip files tutorial

Post 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]
Fidbeck
Posts: 147
Joined: Tue Sep 25, 2012 11:40 pm

Re: Jacek's Create Zip files tutorial

Post 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
Post Reply