如何让PHP ZipArchive使用变量

时间:2016-09-19 05:15:26

标签: php ziparchive

我现在已经敲了一两天了。 http://php.net/manual/en/ziparchive.extractto.php非常简单。我一直遇到的问题是试图获得

 $path = 'zipfile.zip'

 $zip = new ZipArchive;
 if ($zip->open($path) === true) {
  for($i = 0; $i < $zip->numFiles; $i++) {
    $filename = $zip->getNameIndex($i);
    $fileinfo = pathinfo($filename);
    copy("zip://".$path."#".$filename,     "/your/new/destination/".$fileinfo['basename']);
   }                   
   $zip->close();                   
 }

 ?>

接受变量。

  $r = "update/".$fileName;
  echo $r;
 $z = new ZipArchive;
    if($z->open('$r') === true){

   $z->extractTo('update/tmp');

   $z->close();
   return 'ok<br>';
   } else {
   return 'Failed';
  }

我注意到在留言板上的所有示例中,甚至在PHP站点中。 zip文件的名称是硬编码的。我已经看到了一个正在使用的变量的例子,但我无法让它工作。

  $r = "myfile";
  $z = new ZipArchive;
 if($z->open("update/".$r.".zip") === true){
      //extractTo($path);
    $z->close();
    print "ok";
  }else{
    print "Failed";
  }

以上是PHP网站上的示例。我的代码很相似,是

  constructor(private updates$: StateUpdates<AppState>) {}

  @Effect() bar$ = this.updates$
    .whenAction(Actions.FOO)
    .map(obj => obj.state.user.isCool)
    .distinctUntilChanged()
    .filter(x => x)
    .map(() => ({ type: Actions.BAR }));

所有标准的东西。但它只有在我对文件名和位置进行硬编码时才有效。有没有人得到它与变量一起工作?

更新:工作原理

似乎整个名称不能作为变量。但如果您将该名称的一部分包含在内,则允许使用。

  constructor(private actions$: Actions) {}

  @Effect() bar$ = this.actions$
    .ofType(ActionTypes.FOO)
    .map((obj: any) => {
      console.log(obj);              // here is action only
      return obj.state.user.isCool   // so it is wrong here
    })
    .distinctUntilChanged()
    .filter(x => x)
    .map(() => ({ type: ActionTypes.BAR }));

1 个答案:

答案 0 :(得分:0)

您可以将zip文件的完整文件路径作为变量提供,如下所示。它会起作用

<?php
  $fileName = "zipfile.zip";
  $r = "ziptest/".$fileName;                //$r = "ziptest/zipfile.zip"; This also accepted.
  echo $r;
 $z = new ZipArchive;
    if($z->open($r) === true){

   $z->extractTo('destination/');

   $z->close();
   print "ok";
   } else {
   print "Failed";
  }
 ?>
相关问题