Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Get root directory in archive

2 views
Skip to first unread message

Andrew Poulos

unread,
Jul 1, 2021, 10:14:22 PM7/1/21
to
Users can upload a archive (zip) to the server. On the server I can use
extractTo to extract the archive's contents.

The problem I have is that users should create an archive of an entire
directory and not just the directory's contents (so that when the
archive is extracted the contents will be in a directory.)

How can I test whether all the files, and any sub-directories within an
archive, are within one directory?

If they are within one directory I would also like the directory's name.

Andrew Poulos

PS I'm a PHP noob

J.O. Aho

unread,
Jul 2, 2021, 3:07:12 AM7/2/21
to

On 02/07/2021 04.14, Andrew Poulos wrote:
> Users can upload a archive (zip) to the server. On the server I can use
> extractTo to extract the archive's contents.
>
> The problem I have is that users should create an archive of an entire
> directory and not just the directory's contents (so that when the
> archive is extracted the contents will be in a directory.)
>
> How can I test whether all the files, and any sub-directories within an
> archive, are within one directory?

You will need to use the getNameIndex to list the files
https://www.php.net/manual/en/ziparchive.getnameindex.php

> If they are within one directory I would also like the directory's name.

you would use dirname to figure out the path, you can use the level to
get the root.
https://www.php.net/manual/en/function.dirname.php

You can store the first files root directory in a variable, then check
if the next file has the same root as the previous one, if they differ,
than you know there is multiple directories. If the variable value is
empty, then you have files directly in the root.

--

//Aho

Andrew Poulos

unread,
Jul 3, 2021, 1:58:19 AM7/3/21
to
Yep, that did it. Thanks.

Andrew Poulos
0 new messages