Search code examples
phpphp-ziparchive

How to get name of zip file [php] [ziparchive]


Given a ZipArchive, how is it possible to get the name of the file that is opened?

Let's say I have library function (which I can't edit) and it looks like this

function getZipFile(): ZipArchive
{
    $zip = new ZipArchive;
    $zip->open("SomeFileNameIDontKnow.zip");
    return $zip;
}

Is there a way to get the filename for me to show a user?

$zip = getZipFile();
print "Opening zip archive " . $zip->functionToGetTheFileName();

Solution

  • Use $zip->filename. From the documentation:

    /* Properties */
    public readonly int $lastId;
    public readonly int $status;
    public readonly int $statusSys;
    public readonly int $numFiles;
    public readonly string $filename;
    public readonly string $comment;
    ...
    filename
        File name in the file system