简体   繁体   English

在使用ZipArchive()添加到zip之前,如何重命名.txt文件?

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

I am using ZipArchive() to zip a bunch of .txt files together. 我正在使用ZipArchive()将一堆.txt文件压缩在一起。 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? 有人可以举一个例子,在将所有.txt文件放入zip文件之前,如何从它们的开头删除ID吗?

For example: 例如:

  • 144-apples.txt 144 apples.txt
  • 2-oranges.txt 2- oranges.txt
  • 25555-bananas.txt 25555-bananas.txt

will be: 将会:

  • apples.txt apples.txt
  • oranges.txt oranges.txt
  • bananas.txt bananas.txt

in the zip. 在拉链中。

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

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);
    }
?>

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM