douou0977 2014-09-29 17:18
浏览 391
已采纳

如何使用PHP ZipArchive :: addPattern添加相对路径?

As simple as the title:

$zipFileName = $folderName . DIRECTORY_SEPARATOR . strtolower(sprintf('bundle_%s.zip', date('F_Y', strtotime('last month'))));

$zip = new \ZipArchive();
if ($zip->open($zipFileName, \ZIPARCHIVE::CREATE )!==TRUE) {
    throw new \Exception("cannot open <$zipFileName>
", 500);
}

$zip->addPattern('/\.csv$/', $folderName, ['remove_path' => $folderName]);
$zip->close();

This is creating a zip file with the path as the absolute path on the machine:

So I'm opening the resulting zip and having:

/tmp/bundle/file1.csv
/tmp/bundle/file2.csv
/tmp/bundle/file3.csv

But, I would like to get:

file1.csv
file2.csv
file3.csv

Not sure what else to try?

  • 写回答

1条回答 默认 最新

  • douzhen9428 2014-09-29 19:44
    关注
        $zip         = new \ZipArchive();
        if($zip->open($zipFullPath, \ZIPARCHIVE::CREATE) !== TRUE)
        {
            throw new \Exception("Cannot open <$zipFullPath>
    ", 500);
        }
        foreach(glob($folderName . DIRECTORY_SEPARATOR . '*') as $file)
        {
            $zip->addFile($file, basename($file));
        }
        $zip->close();
    

    This is how I ended up doing it. No luck with addPattern function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失