Monday, February 27, 2012

For creating zip file in php for the contents in a directory

This is a sample script for creating a zip file which includes all directories and files in a specified directory.

$files = $this->directoryToArray("$path/assets",TRUE);

$zip = new ZipArchive();
if($zip->open($zip_path,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) === true) {

            foreach($files as $filenames){
                if(!is_dir($filenames)){
                    $filename = substr($filenames,strrpos($filenames,"assets"));
                    $zip->addFile("$filenames","package/$filename");
                }
            }         

           
        }
         $zip->close();   

       
        header("Content-Type: application/force-download");
        header("Content-Disposition: attachment; filename=package.zip");
        readfile($zip_path);
--------------------------------------------------------------------------
function directoryToArray($directory, $recursive) {
        $array_items = array();
        if ($handle = opendir($directory)) {
            while (false !== ($file = readdir($handle))) {
                if ($file != "." && $file != "..") {
                    if (is_dir($directory. "/" . $file)) {
                        if($recursive) {
                            $array_items = array_merge($array_items, $this->directoryToArray($directory. "/" . $file, $recursive));
                        }
                        $file = $directory . "/" . $file;
                        $array_items[] = preg_replace("/\/\//si", "/", $file);
                    } else {
                        $file = $directory . "/" . $file;
                        $array_items[] = preg_replace("/\/\//si", "/", $file);
                    }
                }
            }
            closedir($handle);
        }
        return $array_items;
    }

For getting the files and folders in a directory

This is a useful function for getting all subdirectories and files in an array.

$site_files = directoryToArray("path to directory",TRUE);

function directoryToArray($directory, $recursive) {
        $array_items = array();
        if ($handle = opendir($directory)) {
            while (false !== ($file = readdir($handle))) {
                if ($file != "." && $file != "..") {
                    if (is_dir($directory. "/" . $file)) {
                        if($recursive) {
                            $array_items = array_merge($array_items,directoryToArray($directory. "/" . $file, $recursive));
                        }
                        $file = $directory . "/" . $file;
                        $array_items[] = preg_replace("/\/\//si", "/", $file);
                    } else {
                        $file = $directory . "/" . $file;
                        $array_items[] = preg_replace("/\/\//si", "/", $file);
                    }
                }
            }
            closedir($handle);
        }
        return $array_items;
    }

Monday, February 20, 2012

Function for getting all parents of a child category in php

Function for getting all parents of a child category in php

function getRootParent($id,$cats=array())
    {
        $this->db->select('cat_id,cat_parent_id');
        $this->db->where('cat_id', $id);
        $result = $this->db->get('category');
            foreach($result->result_array() as $ids){
                if($ids['cat_parent_id']==0){
                $cats[] = $ids['cat_id'];
                    return $cats;
                }else{
                $cats[] = $ids['cat_id'];
                    $cats = $this->getRootParent($ids['cat_parent_id'],$cats);
                }
            }
      
        return $cats;
    }