Cofey
Cofey

Reputation: 11404

How to rename .txt files before being added to zip using ZipArchive()?

I am using ZipArchive() to zip a bunch of .txt files together. Can someone please give me an example how to remove the ID from the beginning of all .txt files before placing them in the zip?

For example:

will be:

in the zip.

$name = str_replace(TXT_FILE_DIRECTORY . $group . '/', '', $file);
$zip->addFile($file, $name);

Upvotes: 0

Views: 1260

Answers (2)

h2ooooooo
h2ooooooo

Reputation: 39532

Something like this should work:

<?php
    //Your $files array creation logic here
    foreach ($files as $file) {
        $fileName = preg_replace("/^\d+\-/", "", str_replace(TXT_FILE_DIRECTORY . $group . '/', '', $file));
        $zip->addFile($file, $fileName);
    }
?>

Upvotes: 4

Walter Caraza
Walter Caraza

Reputation: 911

maybe you can serve the following code:

    $this->zipfile->add_dir(FCPATH."files/");
    for($u=$ini;$u<=$do;$u++){
        $this->zipfile->add_file(implode("",file(FCPATH."files/batchsql_".$u.".txt")), FCPATH."files/batchsql_".$u.".txt");
    }

    header("Content-type: application/octet-stream");
    header("Content-disposition: attachment; filename=zipfile.zip");
    echo $this->zipfile->file();

Upvotes: 0

Related Questions