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条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘