Discussion:
Get root directory in archive
(too old to reply)
Andrew Poulos
2021-07-02 02:14:14 UTC
Permalink
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
2021-07-02 07:07:10 UTC
Permalink
Post by Andrew Poulos
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
Post by Andrew Poulos
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
2021-07-03 05:58:12 UTC
Permalink
Post by J.O. Aho
Post by Andrew Poulos
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
Post by Andrew Poulos
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.
Yep, that did it. Thanks.

Andrew Poulos

Loading...