Icy Phoenix

     
 



Post new topic  Reply to topic 
Page 1 of 1
 
 
Reply with quote Download Post
Post Batch ZIP Folders Using PHP 
 
I wanted to share this you this small snippet of code which I use to compress my styles before uploading here and submitting on phpbb.com for approval.

Since I have designed several styles over the year, I get a bit annoyed when I need to pack up those styles for a new release... that's where PHP comes to help me.

A quick example could help in understanding:
  • I have several styles in one folder:
    • black_pearl
    • frozen_phoenix
    • mg_autumn
    • mg_xmas
    • milky_way
    • milky_way_red
    • rainbow_pearl

  • I need to create a zip file for each of the above folders in a format like this:
    • black_pearl_1_6_4_20110603.zip
    • frozen_phoenix_1_6_4_20110603.zip
    • mg_autumn_1_6_4_20110603.zip
    • mg_xmas_1_6_4_20110603.zip
    • milky_way_1_6_4_20110603.zip
    • milky_way_red_1_6_4_20110603.zip
    • rainbow_pearl_1_6_4_20110603.zip

  • To do the job I just launch a PHP script


Here are the basic instructions:
  1. To be able to use this script you need pclzip.lib.php library which you will find in Icy Phoenix package or here: PCLZIP
  2. Create a folder where you will put all subfolders to be zipped in different ZIP files, suppose you call this folder backup.
  3. Put in the folder you just created pclzip.lib.php file
  4. Put in the folder you just created all the folders you want to be zipped into separate files
  5. Put this file (make_zip.php) in that folder (highlighted in red you will find the parts you may want to change):
    Code: (make_zip.php) [Download] [Hide] [Select]
    <?php
    /**
    *
    * @package Icy Phoenix
    * @version $Id$
    * @copyright (c) 2008 Icy Phoenix
    * @license http://opensource.org/licenses/GPL-license.php GNU Public License
    *
    */

    include('pclzip.lib.php');

    @set_time_limit(0);
    //@ignore_user_abort(true);
    @ini_set('memory_limit', '128M');

    $style_version = '_1_6_4';
    $date_suffix = '_' . date('Ymd');
    $source_array = array('black_pearl', 'frozen_phoenix', 'mg_autumn', 'mg_xmas', 'milky_way', 'milky_way_red', 'rainbow_pearl');

    output_header('Creating Styles ZIP Packs');

    $styles_counter = count($source_array);
    for ($i = 0; $i < $styles_counter; $i++)
    {
        $dest_path = $source_array[$i] . '/' . $source_array[$i];
        echo('<hr /><b style="color:#dd2222;">Working on ' . $source_array[$i] . '</b><br />');

        // Kill BAK
        echo('<b style="color:#224488;">Deleting bak files</b>');
        flush();
        $bak_files = array();
        $bak_files = list_files_type($dest_path, 'bak');
        for ($j = 0; $j < count($bak_files); $j++)
        {
            @unlink($bak_files[$j]);
        }
        echo(' ... <b style="color:#228822;">DONE!!!</b><br />');
        flush();

        // Create ZIP package
        $zip_filename = 'phpbb3_' . $source_array[$i] . $style_version . '.zip';
        echo('<b style="color:#224488;">Creating ' . $zip_filename . '</b>');
        flush();
        if (file_exists($zip_filename))
        {
            @unlink($zip_filename);
        }
        $all_files_list = array();
        $all_files_list = list_files($dest_path);
        if (isset($zip))
        {
            unset($zip);
        }
        $zip = new PclZip($zip_filename);
        @chmod($zip_filename, 0777);
        $zip->add($all_files_list, $p_add_dir = '', $p_remove_dir = $source_array[$i]);
        echo(' ... <b style="color:#228822;">DONE!!!</b><br /><hr />');
        flush();
    }

    output_footer();
    exit;

    function output_header($page_title = 'Files List')
    {
        echo('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . "\n");
        echo('<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-gb" xml:lang="en-gb">' . "\n");
        echo('<head><title>' . $page_title . '</title></head>' . "\n");
        echo('<body style="font-family:\'Courier New\', \'Trebuchet MS\', Verdana, Tahoma;font-size:12px;">' . "\n");
        flush();
        return true;
    }

    function output_footer()
    {
        echo('</body>' . "\n" . '</html>');
        flush();
        return true;
    }

    function list_files($source_folder)
    {
        $directory = @opendir($source_folder);
        $files_list = array();
        while (@$file = readdir($directory))
        {
            $full_path_file = $source_folder . '/' . $file;
            if (!in_array($file, array('.', '..')))
            {
                if (is_dir($source_folder . '/' . $file))
                {
                    $files_list = array_merge($files_list, list_files($full_path_file));
                }
                else
                {
                    $files_list[] = str_replace('//', '/', ($full_path_file));
                }
            }
        }
        @closedir($directory);
        sort($files_list);
        return $files_list;
    }

    function list_files_type($source_folder, $extension)
    {
        $directory = @opendir($source_folder);
        $files_list = array();
        while (@$file = readdir($directory))
        {
            $full_path_file = $source_folder . '/' . $file;
            if (!in_array($file, array('.', '..')))
            {
                if (is_dir($source_folder . '/' . $file))
                {
                    $files_list = array_merge($files_list, list_files_type($full_path_file, $extension));
                }
                else
                {
                    $file_details = get_file_details($full_path_file);
                    if ($file_details['ext'] == $extension)
                    {
                        $files_list[] = str_replace('//', '/', ($full_path_file));
                    }
                }
            }
        }
        @closedir($directory);
        sort($files_list);
        return $files_list;
    }

    function get_file_details($file_name)
    {
        $file_details = array();
        $file_tmp = str_replace ('http://', '', $file_name);
        $file_path[] = array();
        $file_path = explode('/', $file_tmp);
        $file_details['name_full'] = $file_path[count($file_path) - 1];
        $file_part = explode('.', strtolower($file_details['name_full']));
        $file_details['ext'] = $file_part[count($file_part) - 1];
        $file_details['name'] = substr($file_details['name_full'], 0, strlen($file_details['name_full']) - strlen($file_details['ext']) - 1);
        return $file_details;
    }

    ?>

  6. You just need to make sure you edit this portion of code including a version number (if you want, otherwise you can leave that empty!) and listing all the folders you want to be zipped... timestamp will be added automatically:
    Code: [Download] [Hide] [Select]
    $style_version = '_1_6_4';
    $date_suffix = '_' . date('Ymd');
    $source_array = array('black_pearl', 'frozen_phoenix', 'mg_autumn', 'mg_xmas', 'milky_way', 'milky_way_red', 'rainbow_pearl');

  7. If you did everything correctly, by launching make_zip.php all ZIP files will be created for the listed folders.
  8. Remember to CHMOD 0777 the backup folder (the folder where you put all the folders and make_zip.php).


I hope you will find this snippet and quick tutorial useful.
 




____________
Luca
SEARCH is the quickest way to get support.
Icy Phoenix ColorizeIt - CustomIcy - HON
 
Mighty GorgonSend private messageSend e-mail to userVisit poster's website  
Back to topPage bottom
Icy Phoenix is an open source project, you can show your appreciation and support future development by donating to the project.

Support us
 
Post new topic  Reply to topic  Page 1 of 1
 


Display posts from previous:    

HideWas this topic useful?

Link this topic
URL
BBCode
HTML




 
Permissions List
You cannot post new topics
You cannot reply to topics
You cannot edit your posts
You cannot delete your posts
You cannot vote in polls
You cannot attach files
You can download files
You cannot post calendar events


  

 

  cron