dongmeixi5311 2016-06-01 15:39
浏览 107

PHP ZipArchive getFromName无法读取具有相对路径的文件

I'm trying to read a file content from a zip file with ZipArchive with the method getFromName. My file name has a double dot in it (..). Here's my code:

$zip = new ZipArchive();

$zip->open('book.zip');

$content = $zip->getFromName('book/html/../README.md');

var_dump($content);

The structure of my zip file is this:

Archive:  book.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2016-06-01 11:09   book/
        0  2016-06-01 11:02   book/html/
     1855  2016-06-01 11:02   book/html/composer.json
     2354  2016-06-01 11:09   book/README.md
---------                     -------
     4209                     4 files

My PHP script is in the same directory as my zip file. When I try to list the file in the folder that I have zipped with Linux command like this:

ls -l book/html/../README.md 

It works perfectly. I don't understand why ZipArchive can't read this relative path correctly.

Note: I know that I can read the file with the absolute path: book/README.md but I really need to use the relative path with .. because the file paths are loaded dynamically from another place and I don't have a control on them.

  • 写回答

1条回答 默认 最新

  • doulong2782 2016-09-12 12:13
    关注

    I spent a day or so on a similar issue trying to read relative file paths including ".." change directories. ZipArchive is unable to handle those paths if the file is not extracted. The best solution I came up with without having to extract the Zip, was to use the following function to get the full path for the file you want: http://php.net/manual/en/function.realpath.php#84012. After getting the full file path, ZipArchive::getFromName() will be able to find the file you're looking for.

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line