简体   繁体   English

读取位于zip存档中的php文件

[英]Read php file located in zip archive

I have been trying to read a PHP file inside a ZIP archive. 我一直在尝试读取ZIP归档文件中的PHP文件。 I have coded the following code, which can read text documents and echo without errors, but when I tested it with a PHP file, nothing appear. 我已经编写了以下代码,该代码可以读取文本文档并回显而没有错误,但是当我使用PHP文件对其进行测试时,什么也没有出现。 So what can I do to read the PHP file without extracting? 那么,如何在不解压的情况下读取PHP文件呢?

<?php
$zip = zip_open("test.zip");
$filename= "test.php";

if (is_resource($zip))
{
while ($zip_entry = zip_read($zip))
{
    if (zip_entry_open($zip, $zip_entry) && zip_entry_name($zip_entry) == $filename)
    {
        echo "Name: " . zip_entry_name($zip_entry) . "<br />";
        echo "<p>";
        echo "File Contents:<br/>";
        $contents = zip_entry_read($zip_entry);
        echo "$contents<br />";
        zip_entry_close($zip_entry);
    }
}
zip_close($zip);
}

Thanks in advance! 提前致谢!

没有很多资源可以使用默认的zip_read()php函数,但是这个https://github.com/Ne-Lexa/php-zip库使在php中使用zip文件变得轻而易举,您应该检查一下出来。

You can use stream wrappers to read a file directly from within a zip, in one line, without extracting anything to disk. 您可以使用流包装器直接从zip内一行读取文件,而无需将任何内容提取到磁盘。 For example: 例如:

$str = file_get_contents('zip://test.zip#test.php');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM