php and zip question

Discussion in 'PHP' started by stephan2307, Nov 24, 2010.

  1. #1
    Hi,

    I use php to zip up the content of a folder. That works fine. But because of where the script and the subfolder are located in relation to each other I end up with loads of folders in the zip but I only want the files in there. Any help would be great.

    The structure is like this.

    when I open the zip all of the folder and subfolders are in the zip file as well but I only want to have the files in the zip folder.

    Any help?
     
    stephan2307, Nov 24, 2010 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    I would have to see the script you are using.

    If you want to add all file files to one folder, removing their folder relationship, you would just keep adding the files to the zip archive and make sure there are no addEmptyDir functions called.
     
    ThePHPMaster, Nov 24, 2010 IP
  3. REXTEX

    REXTEX Guest

    Messages:
    64
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    How about putting the files you want sipped in their own folder and zip that folder.
     
    REXTEX, Nov 26, 2010 IP
  4. drctaccess

    drctaccess Peon

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #4
    if you have ZIP module compiled in your php then you can do it this way:

    <?php
    $zip = new ZipArchive;
    if ($zip->open('output.zip') === TRUE) {
    $zip->addFile('/path/to/file1.jpg', 'file1.jpg');
    $zip->addFile('/path/to/file2.jpg', 'file1.jpg');
    $zip->addFile('/path/to/file3.jpg', 'file1.jpg');
    $zip->close();
    echo 'ok';
    } else {
    echo 'failed';
    }
    ?>


    if you are in an *NIX environment and it has the ZIP package installed .. you can do it this way
    <?php

    exec('/usr/bin/zip -r -D output.zip folder');

    //Note: When safe mode is enabled, you can only execute files within the safe_mode_exec_dir. For practical reasons, it is currently not allowed to have .. components in the path to the executable.

    ?>
     
    drctaccess, Nov 26, 2010 IP