douang4294 2014-12-10 15:59
浏览 133
已采纳

PHP file_get_contents无法在Windows上读取zip

I've searched all over for a solution to this and I can't find one.

I'm doing something like the following:

$data = file_get_contents('zip://../files/' . $this->_file->getHash() . '.zip#' . $stat['name']);

The specific error is:

failed to open stream: operation failed

It works on a Linux machine I use, but I'm trying to get the project running on a Windows box. I know that the path to the zip file is correct and it is readable because in the same code that the above snippet is located in, I read it using the ZipArchive class, like this:

$this->_za = new ZipArchive();
$res = $this->_za->open('../files/' . $file->getHash() . '.zip');

And that works perfectly fine.

Why can't file_get_contents read from the zip? Is there something I have to do to the files directory? I'm not very good at folders and permissions on Windows.

Edit: to be absolute sure it wasn't an issue with my code, I set up a very simple test script, with a zip file in the exact same directory:

<?php

$za = new ZipArchive();
$res = $za->open('myZip.zip');

for($i=0; $i<$za->numFiles; $i++)
{
    $stat = $za->statIndex($i);
    $data = file_get_contents('zip://myZip.zip#' . $stat['name']);

    echo $stat['name'];
    echo $data;
}

This outputs the name of each file in the zip (from the echo $stat['name'] part) but then produces the exact same error as before, i.e.,

 failed to open stream: operation failed
  • 写回答

3条回答 默认 最新

  • dougang6821 2014-12-13 08:47
    关注

    The fix was reasonably simple. Instead of:

    $data = file_get_contents('zip://myZip.zip#' . $stat['name']);
    

    If I do:

    $data = file_get_contents('zip://' . realpath('myZip.zip') . '#' . $stat['name']);
    

    It works.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA