在 PHP 中使用 ZIP 命令删除文件夹结构(仅限父级)

Remove folder structure (parents only) using ZIP command in PHP

我想在 X 文件夹中创建一个名为 filename.zip 的所有文件的 zip,在 php 中使用以下命令,例如:

exec(zip -r "./Zips/filename.zip" "./Uploads/Data/X/")

但创建的 zip 具有文件夹结构 Uploads/Data/X。请帮助我摆脱这些父文件夹 - Uploads/Data.

Ignoring folder structure when creating zip archive (Linux):
By default, zip will store the full path (relative to the current directory).

几种方法:

  • 预先移动到 X 文件夹:

    exec('cd /./Uploads/Data/X/; zip -r "./Zips/filename.zip" "*"');
    
  • 使用 -j (--junk-paths) 选项:

    exec('zip -j "./Zips/filename.zip" "./Uploads/Data/X/"');