【问题标题】:Add files and directory to a specific sub directory将文件和目录添加到特定的子目录
【发布时间】:2017-02-03 22:32:29
【问题描述】:

我正在使用以下脚本将我的目录(在本例中为 My_Theme)的文件移动到 zip 存档 wordpress.zip

define('CLIENT_PATH', $_SERVER['DOCUMENT_ROOT'] . '/wp_theme/clients_templates/' . str_replace(' ', '_', $_POST['title']));
$zip = new ZipArchive;
$zip->open('wordpress.zip', ZipArchive::CREATE);
foreach (glob(CLIENT_PATH . "/*.*") as $file) {
    echo $file . '<br>';
   $zip->addFile($file);
}
$zip->close();

现在,当我下载并解压缩该文件时,我的文件夹结构如下所示:

我想要的是将目录My_Theme移动到wordpress/wp-content/themes/

结果将是:wordpress/wp-content/themes/My_Theme(包括其中的所有文件和子目录)

我该怎么做?

【问题讨论】:

  • 你的意思是代码,对吧?
  • 还有。您正在手动解压缩 zip,所以移动过程需要压缩还是解压缩?
  • @AsierPaz 我不需要在网络服务器上解压缩文件。用户将其下载到他的计算机上后,即可解压缩。是的,该目录应该使用代码添加到 zip 存档的特定目录中,而不是手动添加。

标签: php zip ziparchive


【解决方案1】:

我回答我自己的问题,答案很简单:只需定义第二个参数: $zip-&gt;addFile($file, 'wordpress/wp-content/themes/' . $theme_name . '/' . $file_name);

【讨论】:

    【解决方案2】:

    您可以使用http://php.net/manual/en/function.rename.php。这应该可以满足您的需求。

    【讨论】:

    • 为什么是rename()?这只会重命名目录。不是我想要的。
    • @Reza rename("home/pomcanys/public_html/wp_theme/clients_templates/My_Theme","wordpress/wp-content/themes/My_Theme");
    • 我试过用它。如您在此处看到的那样,结果看起来并没有好多少:i.imgur.com/Di8phY7.png
    猜你喜欢
    • 2019-11-01
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    • 2018-09-10
    • 2020-04-21
    • 1970-01-01
    • 1970-01-01
    • 2011-11-06
    相关资源
    最近更新 更多