Codeigniter - Get several files, rename them and then zip it Codeigniter - Get several files, rename them and then zip it codeigniter codeigniter

Codeigniter - Get several files, rename them and then zip it


CodeIgniter's Zip Class apparently does not offer any means to rename entries. You can use PHP's native Zip Extension, which allows you to change the name when adding the file to the archive (and also later).

Example from PHP Manual

$zip = new ZipArchive;if ($zip->open('test.zip') === TRUE) {    $zip->addFile('/path/to/index.txt', 'newname.txt');    $zip->close();    echo 'ok';} else {    echo 'failed';}


Thanks to @Gordon 's answer, I found a solution.He is completely right about Codeigniter not being able to rename a file, but I found a really quick change to the library and it seems to be working.

If you go to your system>librairies->Zip.php like mentioned by @Gordon, search for "read_file" and you'll find the function.

Then I simply added a argument to the function and modified some of the code thereafter, see below:

function read_file($path, $preserve_filepath = FALSE, $name = NULL) // Added $name{    if ( ! file_exists($path))    {        return FALSE;    }    if (FALSE !== ($data = file_get_contents($path)))    {            if($name == NULL){  // Added a verification to see if it is set, if not set, then it does it's normal thing, if it is set, it uses the defined var.        $name = str_replace("\\", "/", $path);        if ($preserve_filepath === FALSE)        {            $name = preg_replace("|.*/(.+)|", "\\1", $name);        }            }        $this->add_data($name, $data);        return TRUE;    }    return FALSE;}

I hope this helps others. Thanks again @Gordon