Reputation: 61
I see in php.net and in this forum this for include files from zip file :
<?php
include ("zip://./test.zip#file.php");
?>
I create zip file called test.zip and inside put other file called file.php
The people say , this let include file.php inside zip file in other php file
I try all time and tell me error document no exists
Warning: include(zip://test.zip#file.php) [function.include]: failed to open stream: No such file or directory in C:\AppServ\www\zip\zip.php on line 2
Warning: include() [function.include]: Failed opening 'zip://test.zip#file.php' for inclusion (include_path='.;C:\php5\pear') in C:\AppServ\www\zip\zip.php on line 2
The people tell this it´s right but for me , never works , i don´t know if i put something bad or need other thing
The Best Regards
Upvotes: 2
Views: 1785
Reputation: 8405
Warning: This cannot be done in memory — ZipArchive
cannot work with "memory mapped files".
A note about the below, with power comes responsibility and it is up to each of us to ensure that no unfiltered user input ever ends up in eval().
You can obtain the data of a file inside a zip-file into a variable (memory) with file_get_contents
Docs as it supports the zip://
Stream wrapper Docs:
$zipFile = './test.zip'; # path of zip-file
$fileInZip = 'file.php'; # name the file to obtain
# read the file's data:
$path = sprintf('zip://%s#%s', $zipFile, $fileInZip);
$fileData = file_get_contents($path);
eval($fileData);
You can only access local files with zip://
or via ZipArchive. For that you can first copy the contents to a temporary file and work with it:
$zip = 'http://www.domain.com/test.zip';
$file = 'file.php';
$ext = pathinfo($zip, PATHINFO_EXTENSION);
$temp = tempnam(sys_get_temp_dir(), $ext);
copy($zip, $temp);
$data = file_get_contents("zip://$temp#$file");
unlink($temp);
eval($data);
Or you can get this via stream with
$fp = $zip->getStream('file.php');
if(!$fp) exit("failed\n");
while (!feof($fp)) {
$contents .= fread($fp, 1024);
}
fclose($fp);
eval($contents);
PLEASE KEEP THE FOLLOWING IN MIND:
If eval() is the answer, you're almost certainly asking the wrong question. -- Rasmus Lerdorf, BDFL of PHP
Upvotes: 0
Reputation: 86
The following code works without any issues:
page.php
<?php
include("zip://./include.zip#include_me.php");
?>
include_me.php
echo "File was included successfully!";
The issue you are encountering would suggest that there is an issue with the ZIP file itself. A common mistake to make is for the ZIP file to include a directory, and for all zipped files to be included within said directory.
I would recommend double-checking your ZIP file to ensure that just the file was zipped, and not the file and the directory.
If the directory has accidentally been included, your file might be located somewhere like zip://./test.zip#test/file.php
Upvotes: 1