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

如何使用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?

图片转代码服务由CSDN问答提供 功能建议

简单如标题:

  $ zipFileName = $ 文件夹名称 。  DIRECTORY_SEPARATOR。  strtolower(sprintf('bundle_%s.zip',日期('F_Y',strtotime('上个月')))); 
 
 $ zip = new \ ZipArchive(); 
if($ zip-&gt;  open($ zipFileName,\ ZIPARCHIVE :: CREATE)!== TRUE){
 throw new \ Exception(“无法打开&lt; $ zipFileName&gt; 
”,500); 
} 
 
 $ zip-&gt  ; addPattern('/ \ .csv $ /',$ folderName,['remove_path'=&gt; $ folderName]); 
 $ zip-&gt; close(); 
   
  
 

这是创建一个zip文件,路径作为机器上的绝对路径:

所以我打开生成的zip并输入:

 <代码> /tmp/bundle/file1.csv
/tmp/bundle/file2.csv
/tmp/bundle/file3.csv
   
 
  

但是,我想得到:

  file1.csv 
file2.csv 
file3.csv 
   
 
  

不确定还有什么要尝试?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题