Welcome Guest, Not a member yet? Register   Sign In
Having trouble with the zip library
#1

[eluser]louisl[/eluser]
I'm creating a zip file to download.

Code:
$this->zip->read_dir(SITE_PATH.'/media/feeds/rightmove/', FALSE, '/');

Then downloading with:-

$this->zip-download($this->BRANCH_ID.date('Ymd').'01.zip');

To my understanding and what I'm trying to achive is that this should download a zip file that when opened would have no diectories and just expand the files.

The zip file name is fine eg. 99XX992011042701.zip and downloads, but the files inside all become prefixed with the server path minus slashes eg.

nfs_securejmysitenamemediafeedsrightmove99XX99_416_IMG_00.jpg instead of the intended image name 99XX99_416_IMG_00.jpg

I should probaly note most of this job isn't in CodeIgniter but I'm using the CI zip library class and download helper with the intention of refactoring to CI later.

Anyone have any ideas on this?
#2

[eluser]astrofire[/eluser]
I have a very similar problem. Thanks for any advice!
#3

[eluser]louisl[/eluser]
I've got it working with a rough dirty fix, I need to spend more time on this but for now...

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
* MY_Zip.php
* Dirty fix for zip files adding path names to files when you don't want directory structure to be preserved.
* I'm not sure if this will break other CI Zip methods. Needs more investigation.
*/

class MY_Zip extends CI_Zip {

     public function __construct()
    {
        parent::__construct();
    }

    /**
     * Read a directory and add it to the zip.
     *
     * This function recursively reads a folder and everything it contains (including
     * sub-folders) and creates a zip based on it.  Whatever directory structure
     * is in the original file path will be recreated in the zip file.
     *
     * @access    public
     * @param    string    path to source
     * @return    bool
     */
    
    
    
    function read_dir($path, $preserve_filepath = TRUE, $root_path = NULL)
    {
        if ( ! $fp = @opendir($path))
        {
            return FALSE;
        }

        // Set the original directory root for child dir's to use as relative
        if ($root_path === NULL)
        {
            $root_path = dirname($path).'/';
        }

        while (FALSE !== ($file = readdir($fp)))
        {
            if (substr($file, 0, 1) == '.')
            {
                continue;
            }

            if (@is_dir($path.$file))
            {
                $this->read_dir($path.$file."/", $preserve_filepath, $root_path);
            }
            else
            {
                if (FALSE !== ($data = file_get_contents($path.$file)))
                {
                    $name = str_replace("\\", "/", $path);
                    
                    if ($preserve_filepath === FALSE)
                    {
                        $name = str_replace($root_path, '', $name);
                    }

                    //$this->add_data($name.$file, $data); // comment this
                    $this->add_data($file, $data); // add this
                }
            }
        }

        return TRUE;
    }

}    
/* End of file MY_Zip.php */
/* Location: ./application/libraries/MY_Zip.php */




Theme © iAndrew 2016 - Forum software by © MyBB