Operating with zip file in PHP [closed]

2019-09-21 12:35发布

Inside webserver, I'm downloading zip and unziping with function below.

$zip = new ZipArchive;
$res = $zip->open('tip.zip');
if ($res === TRUE) {
    $zip->extractTo("$dest/");
    $zip->close();
} else {
    die('ZIP not supported on this server!');
}

The problem is, when I unzip with this script, I am getting the folder inside zip (there is folder inside zip). But, I need to get files, folders inside zip's child folder, not folder itself.

In other words

enter image description here

So I need:

  • to execute some function like moveRecursively("$username-$reponame-$node", $destination); where $destination is root and then delete this folder
  • or do this action somehow while unziping and unzip driectly child folders contents into folder where script located (public_html in my case)

Can't figure out solution. Please help.

1条回答
Root(大扎)
2楼-- · 2019-09-21 12:45

This should be actual obvious.

Pass the name of that folder to your script (next to the name of the zip file), then move all files from inside that folder (after you extracted them) to the wanted destination:

$zip = new ZipArchive;
$res = $zip->open('tip.zip');
if ($res !== TRUE) {
    die('ZIP not supported on this server!');
}

$zip->extractTo("$dest/");
$zip->close();
rename("$dest/dirname/", "$dest/");
查看更多
登录 后发表回答