Skip to content
Advertisement

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

I want to create a zip of all files in X folder with the name filename.zip using following command in php like:

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

but the created zip has the folder structure Uploads/Data/X. Please help me to get rid of these parent folders – Uploads/Data.

Advertisement

Answer

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

Few approaches:

  • moving to X folder beforehand:

    exec('cd /./Uploads/Data/X/; zip -r "./Zips/filename.zip" "*"');
    
  • using -j (--junk-paths) option:

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