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

如何使用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 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试