在PHP中使用zip文件进行操作 [英] Operating with zip file in PHP

查看:107
本文介绍了在PHP中使用zip文件进行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在网络服务器内部,我正在使用以下功能下载zip和解压缩文件.

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!');
}

问题是,当我使用此脚本解压缩时,我正在zip内找到文件夹(zip内有文件夹).但是,我需要获取文件,而不是zip的子文件夹中的文件夹,而不是文件夹本身.

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.

换句话说

所以我需要:

  • 执行诸如moveRecursively("$ username- $ reponame- $ node",$ destination)之类的功能; $ destination是根目录,然后删除该文件夹
  • 或以某种方式执行此操作,同时将子文件夹的内容解压缩和解压缩到脚本所在的文件夹中(在我的情况下为public_html)

找不到解决方案.请帮忙.

Can't figure out solution. Please help.

推荐答案

这应该是显而易见的.

将该文件夹的名称传递到脚本中(在zip文件的名称旁边),然后将所有文件从该文件夹中移动(提取后)到所需的目的地:

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/");

这篇关于在PHP中使用zip文件进行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆